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:11 PM Sean Conner <sean@conman.org> wrote:
>
> 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).

No, it's correct. It shows two return values: "foo" and nil. nil as
the second return value indicates no semicolon or number, but "foo" as
the first return value clearly indicates that it was found. Note that
the invalid strings further down return just a single nil. So with
this solution, truthiness of the first return value indicates a valid
or invalid string, and in the case of a valid string, truthiness of
the second return value indicates the presence or lack of a semicolon
and number.