I've written a regex to match the below string type which is working as expected when I check it online in regex matcher-
"['432', '212']"
regex - "(\[)('([^']|'')*'), ('([^']|'')*')(])"
ngx.re.find(string, "\"(\[)('([^']|'')*'), ('([^']|'')*')(])\"", "jo")
When I'm using this inside lua block to match the string it's giving me invalid escape sequence error. i escaped the double quotes and tried to escape the special the character in regex as well with the \ but still, the issue persists. any pointer will help. Thanks!
I'll restate what people said in the comments. You've used \[
in your regex, which is a quoted string. In a quoted string, a backslash begins an escape sequence, but \[
is an invalid escape sequence (see the Lua 5.1 manual for valid escape sequences), so the Lua parser is complaining about it. Vanilla Lua 5.1 just removes the backslash (which would be bad in this regex), while Lua 5.3 and LuaJIT complain about it.
You can remove the parse error and make sure that the backslash is actually inserted into the string by escaping it with another backslash – "\\["
– as you would have to do in JavaScript when using the RegExp
constructor, or by using a long string, which doesn't interpret escape sequences – [[\[]]
. If you use a long string, you also have to replace escaped double quotes \"
in your regular expression with just plain "
.