lua-users home
lua-l archive

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


It was thus said that the Great Egor Skriptunoff once stated:
> 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]$

  WOW!  

> 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

  Yeah ... that does work.  And I'll probably use it but ... wow!  I just
wish it wasn't so hideous looking.  Thank you.

  -spc (And now I remember why I like LPEG over Lua patterns ... )