本文介绍了转义gsub的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了一个文件:

local logfile = io.open("log.txt", "r")
data = logfile:read("*a")
print(data)

输出:

...
"(\.)\n(\w)", r"\1 \2"
"\n[^\t]", "", x, re.S
...

是的,日志文件看起来很糟糕,因为它充满了各种命令

Yes, logfile looks awful as it's full of various commands

如何调用gsub并从数据变量中删除"(\.)\n(\w)", r"\1 \2"行?

How can I call gsub and remove i.e. "(\.)\n(\w)", r"\1 \2" line from data variable?

以下代码段不起作用:

s='"(\.)\n(\w)", r"\1 \2"'
data=data:gsub(s, '')

我想需要做一些转义.有什么简单的解决方法吗?

I guess some escaping needs to be done. Any easy solution?

更新:

local data = [["(\.)\n(\w)", r"\1 \2"
"\n[^\t]", "", x, re.S]]

local s = [["(\.)\n(\w)", r"\1 \2"]]

local function esc(x)
   return (x:gsub('%%', '%%%%')
            :gsub('^%^', '%%^')
            :gsub('%$$', '%%$')
            :gsub('%(', '%%(')
            :gsub('%)', '%%)')
            :gsub('%.', '%%.')
            :gsub('%[', '%%[')
            :gsub('%]', '%%]')
            :gsub('%*', '%%*')
            :gsub('%+', '%%+')
            :gsub('%-', '%%-')
            :gsub('%?', '%%?'))
end

print(data:gsub(esc(s), ''))

这似乎很好用,只是我需要转义,转义字符%,因为如果%在匹配的字符串中,它将不起作用.我尝试了:gsub('%%', '%%%%'):gsub('\%', '\%\%'),但是它不起作用.

This seems to works fine, only that I need to escape, escape character %, as it wont work if % is in matched string. I tried :gsub('%%', '%%%%') or :gsub('\%', '\%\%') but it doesn't work.

更新2 :

好吧,如果在我刚刚更正的表格"中首先设置了%,则可以通过这种方式进行转义

OK, % can be escaped this way if set first in above "table" which I just corrected

:糟糕的经历:

更新3 :

^$

如Lua手册所述( 5.1 5.2 5.3 )

As stated in Lua manual (5.1, 5.2, 5.3)

一个更好的主意是仅在分别找到^$以及字符串的开头或结尾时才对它们进行转义.

So a better idea would be to escape ^ and $ only when they are found (respectively) and the beginning or the end of the string.

Lua 5.1-5.2+不兼容

无需将替换字符串中的每个%加倍.参见 lua用户.

There is no need to double every % in the replacement string. See lua-users.

推荐答案

根据在Lua中编程:

The character `%´ works as an escape for those magic characters. So, '%.' matches a dot; '%%' matches the character `%´ itself. You can use the escape `%´ not only for the magic characters, but also for all other non-alphanumeric characters. When in doubt, play safe and put an escape.

The character `%´ works as an escape for those magic characters. So, '%.' matches a dot; '%%' matches the character `%´ itself. You can use the escape `%´ not only for the magic characters, but also for all other non-alphanumeric characters. When in doubt, play safe and put an escape.

这不是意味着您可以简单地将%放在每个非字母数字字符的前面,就可以了.这也将是未来的证明(在引入新的特殊字符的情况下).像这样:

Doesn't this mean that you can simply put % in front of every non alphanumeric character and be fine. This would also be future proof (in the case that new special characters are introduced). Like this:

function escape_pattern(text)
    return text:gsub("([^%w])", "%%%1")
end

它在Lua 5.3.2上对我有用(仅进行了基本测试).不知道它是否可以与旧版本一起使用.

It worked for me on Lua 5.3.2 (only rudimentary testing was performed). Not sure if it will work with older versions.

这篇关于转义gsub的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 20:27