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?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Aidiakapi
  • 6,034
  • 4
  • 33
  • 62

2 Answers2

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.

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
  • 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 `?` – Aidiakapi Jul 06 '12 at 15:02
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).

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
Mud
  • 28,277
  • 11
  • 59
  • 92