我想替换Lua中的字符串; , .
后面的任何单词,字符,数字或符号("some-word\"
除外)。更像是正则表达式中的'*'选项。在Lua中有没有类似'*'的东西?
示例:
some-word\\test
- > some-word\\###
some-word\\b*ax#@$6;
- > some-word\\###;
some-word\\?foo,>
- > some-word\\###,
我正在使用的代码:
d = "some-word"
p = (tostring(d).."\\[.%c%w%p^%;^%,^%.]+")
c = "###"
s = "testing some-word\\test-2 later some-word\\&^*; some-word\\set_34$ "
print(p)
res = (string.gsub(s,p,c)) print(res)
答案 0 :(得分:2)
(some%-word)\[^;,.%s]*
有效,请注意:
-
是Lua模式中的魔法角色,需要进行转义。some%-word
被()
包围,因此使用%1
捕获。^
在开头用于表示
以下补充。测试:
d = "some%-word"
p = "(" .. d .. ")" .. "\\[^;,.%s]*"
c = "%1###"
s = "testing some-word\\test-2 later some-word\\&^*; some-word\\set_34$ "
print(p)
res = string.gsub(s,p,c)
print(res)
输出:
(some%-word)\[^;,.%s]*
testing some-word### later some-word###; some-word###