lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Tue, Sep 10, 2019 at 6:22 AM Sean Conner wrote:

  I have a pattern that looks like this BNF:

        TEXT    = 'A' - 'Z' / 'a' - 'z'
        DIGIT   = '0' - '9'

        pattern = 1*TEXT [ ';' 1*DIGIT ]

  In English, text, optionally followed by a semicolon and some digits.  So
some valid examples:

        foo
        foo;1
        foo;444

  Invalid examples are

        foo;
        foo23

  If there's a semicolon, it must be followed by a digit; if there's no
semicolon, no digits.

 Am I missing something?


This is the Lua pattern you're searching for:  ^(%a+)%f[;%z];?(%d*)%f[;%z]$


for _, s in ipairs{"foo", "foo;1", "foo;444", "foo;", "foo23"} do
   local text, digit = string.match(s, "^(%a+)%f[;%z];?(%d*)%f[;%z]$")
   print(s, text, digit)
end