[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: A Lua pattern question about optional patterns
- From: Sean Conner <sean@...>
- Date: Tue, 10 Sep 2019 18:08:34 -0400
It was thus said that the Great Roberto Ierusalimschy once stated:
> > I have a pattern that looks like this BNF:
> > TEXT = 'A' - 'Z' / 'a' - 'z'
> > DIGIT = '0' - '9'
> > pattern = 1*TEXT [ ';' 1*DIGIT ]
> Maybe like this?
> local text, sep, digit = string.match(s, "^(%a+)(;?)(%d*)$")
> if not text or (sep == "") ~= (digit == "") then
> error("bla bla bla")
> return text, tonumber(digit)
I'll have to test it. The LPEG pattern I would use would be:
text = R("AZ","az")
digit = R"09"
pattern = C(text^1) * (P";" * (digit^1 / tonumber) + Cc(77))
-- 77 is the default value if no number is given yada yada ...
but that's hard to express in the context of what I'm trying to do (I mean,
*I'm* comfortable using LPEG for this, but not many other people woule be),
but so far, your suggestion is about the best I've seen.