lua-users home
lua-l archive

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


On Fri, Nov 22, 2019 at 5:06 AM szbnwer@gmail.com <szbnwer@gmail.com> wrote:
>
> hi folks! :)
>
>
> i get:
> ```
> hippi@vas:~$ lua5.3
> Lua 5.3.3  Copyright (C) 1994-2016 Lua.org, PUC-Rio
> > print(('aaa x bbb'):match'(.-) x (.*)')
> aaa     bbb
> ```
> (lj 2.0.5 does this as well)
>
> i would expect that the 1st capture should be empty without a `^` at
> the beginning of the pattern, as the manuals (up to lua 5.4) say:
> "a single character class followed by '-', which also matches zero or
> more repetitions of characters in the class. Unlike '*', these
> repetition items will always match the shortest possible sequence;"
>
> am i wrong here?

Regardless of shortest versus longest match, Lua always attempts
matches from left to right. In this case it first attempts a match
from the start of the string and succeeds, so it does not try any
other starting points. So the match returned will always begin with
the leftmost character that can start a match, and this takes priority
over shortest/longest.