[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: A Lua pattern question about optional patterns
- From: Sean Conner <sean@...>
- Date: Tue, 10 Sep 2019 18:10:51 -0400
It was thus said that the Great Peter W A Wood once stated:
> > On 10 Sep 2019, at 18:41, Jonathan Goble <jcgoble3@gmail.com> wrote:
> > On Mon, Sep 9, 2019 at 11:22 PM Sean Conner <sean@conman.org> wrote:
> >>
> >> 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
>
> I also came up with a two-step method but wasn’t sure if it was really
> applicable in the actual use case. My solution does not seem very elegant
> but handles Sean’s five examples properly:
>
> > function double_match (s)
> >> local text = s:match('^(%a+)$')
> >> if text then
> >> return text, nil
> >> else
> >> return s:match('^(%a+);(%d+)')
> >> end
> >> end
> > = double_match('foo')
> foo nil
This is incorrect, "foo" by itself is valid (see above).
> > = double_match('foo;1')
> foo 1
> > = double_match('foo;444')
> foo 444
> > = double_match('foo;')
> nil
> > = double_match('foo23')
> nil
-spc (But yeah, a two-step process it looking like the solution ... sigh)