Lua brackets and 0 or 1 occurrences

I am trying to match a string to a pattern, but there is one thing that I could not figure out. In regex, I would do this:

Strings:
en
eng
engl
engli
englis
english

Pattern:
^en(g(l(i(s(h?)?)?)?)?)?$

I want all the lines to match each other. Compared to the Lua sample, I cannot get this to work.

An even simpler example like this will not work:

Strings:
fly
flying

Pattern:
^fly(ing)?$

Does anyone know how to do this?

+5
source share
2 answers

You can not make groups Conformity optional (or repeat them) using Lua quantifiers ?, *, +and -.

(%d+)? "" ?, , :

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

:

a?

AFAIK, Lua, :

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

() "enh", "enls",....

+8

Lua . .

:

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

, , Lua. LPeg, PEGs, ( , ).

+2
source

All Articles