I want to write a patterns that takes a string like this /a/b/c and extracts a, b, and c. a, b, and c are optional, so /// is a valid input. Currently I have this: "^%/(.-)%/(.-)%/(.-)$". This works, except if my input is /</>/b/c, I get matches: <, >, b/c. Obviously the second / should be escaped like this: /<\\/>/b/c, however this gives me: <\, >, b/c. Is there a way to write this pattern such that /<\\/>/b/c would give me: <\/>, b, c? I know I could change the first .- to a .+ and that would solve this exact issue, but it doesn't solve the larger issue(i.e. what if the escaped slash is in section b).
How to write a lua pattern that is aware of escaped characters?
40 views Asked by Peter R At
2
There are 2 answers
0
On
To the best of my knowledge, it's not possible.
Normally, in POSIX ERE a working regex would be:
^/(?:[^\\/]|\\.)*/(?:[^\\/]|\\.)*/(?:[^\\/]|\\.)*$
...where (?:[^\\/]|\\.) means "either not \ (escaping) and not / (delimiter), or an escaped character".
However, Lua patterns don't have |. Quantifiers are also not applicable for groups. That said, there is no way to differentiate normal and escaped characters.
The solution is to write your own parser from scratch.
It is impossible to achieve using a single Lua pattern, but you can chain a few of them:
Output:
Or, if you want a single statement instead of a loop: