I'm not sure it's a correct regex.
Lua patterns are no regular expressions. Compared to regular expressions Lua's string pattens have a different syntax and are more limited.
Your code
local newpath = s:gsub("(_x)[^_]*(_)", "_static_")
replaces "_x"
followed by 0 or more non-underscore characters followed by "_"
with "_static_"
This is correct but not very elegant.
- the captures
()
are not necessary as you don't make use of them. So "_x[^_]*_"
would achieve the same.
- if you know that only the x12345678 part changes and that there are only digits after x you can simply use
"x%d+"
and repalce it with "static"
. This matches "x"
followed by 1 or more digits. Or you include the underscores.
- if you only want to match exactly 8 digits you could use
"%d%d%d%d%d%d%d%d"
or string.rep("%d",8)