Lua 模式替换后跟特定字符串的任何单词、字符、数字或符号

Lua pattern to replace any word,character, digit or symbol followed by a paticular string

我想替换 Lua 中紧跟字符串 "some-word\" 的任何单词、字符、数字或符号(; , . 除外)。更像是正则表达式中的“*”选项。 Lua中有类似'*'的东西吗?

示例:

some-word\test -> some-word\###

some-word\b*ax#@; -> 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)

(some%-word)\[^;,.%s]* 有效,请注意:

  • -是Lua模式中的魔法字符,需要转义。
  • some%-word()包围,所以用%1.
  • 捕获
  • 字符class中,^开头表示 以下内容的补充。

测试:

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###