I want to replace any word, character, digit or symbol (except ; , .
) followed by the string "some-word\"
in Lua. More like a '*' option in regex. Is there any thing similar to '*' in Lua?
Example:
some-word\\test
-> some-word\\###
some-word\\b*ax#@$6;
-> some-word\\###;
some-word\\?foo,>
-> some-word\\###,
The code I'm using:
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]*
works, note that:
-
is a magic character in Lua patterns, it needs to be escaped.some%-word
is surrounded by ()
, so that it's captured with %1
.^
is used in the beginning to indicate
complement of the following.Test:
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)
Output:
(some%-word)\[^;,.%s]*
testing some-word### later some-word###; some-word###