I’m trying to match a string against a pattern, but there’s one thing I haven’t managed to figure out. In a regex I’d do this:
Strings:
en
eng
engl
engli
englis
english
Pattern:
^en(g(l(i(s(h?)?)?)?)?)?$
I want all strings to be a match.
In Lua pattern matching I can’t get this to work.
Even a simpler example like this won’t work:
Strings:
fly
flying
Pattern:
^fly(ing)?$
Does anybody know how to do this?
You can’t make match-groups optional (or repeat them) using Lua’s quantifiers
?,*,+and-.In the pattern
(%d+)?, the question mark “looses” its special meaning and will simply match the literal?as you can see by executing the following lines of code:which will print:
AFAIK, the closest you can come in Lua would be to use the pattern:
which (of course) matches string like
"enh","enls", … as well.