Lua pattern parentheses and 0 or 1 occurrence
Asked Answered
K

2

8

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?

Kyrstin answered 6/7, 2012 at 14:42 Comment(0)
R
10

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:

text = "a?"
first_match = text:match("((%w+)?)")
print(first_match)

which will print:

a?

AFAIK, the closest you can come in Lua would be to use the pattern:

^eng?l?i?s?h?$

which (of course) matches string like "enh", "enls", ... as well.

Rocha answered 6/7, 2012 at 14:53 Comment(1)
Sadly that kinda looses the meaning, since: enlh doesn't really suit. I'm going to have to workaround this by capturing the rest of the word (%a*) and then checking if it's an allowable 'suffix'. Thanks anyways :) and I already expected this, but thanks for confirming and saying that it matches the literal ?Kyrstin
D
3

In Lua, the parentheses are only used for capturing. They don't create atoms.

The closest you can get to the patterns you want is:

'^flyi?n?g?$'
'^en?g?l?i?s?h?$'

If you need the full power of a regular expression engine, there are bindings to common engines available for Lua. There's also LPeg, a library for creating PEGs, which comes with a regular expression engine as an example (not sure how powerful it is).

Doorframe answered 6/7, 2012 at 14:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.