lua-users home
lua-l archive

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



2017-08-30 17:50 GMT+02:00 Roberto Ierusalimschy <roberto@inf.puc-rio.br>:
>> An old issue on this list [1] was settled in Lua 5.3.3.
>>
>> Up to Lua 5.3.2, string.gmatch and string.gsub had Perl semantics when
>> the pattern couild match an empty string; from Lua 5.3.3 onwards, they
>> have Python semantics.
>> [...]
>>
>> Although mentioned in a list of changes [2], the manual itself does
>> not document the change in behaviour (the old behaviour was not
>> considered to be a bug).
>
> Lua uses the same manual for all releases of a version, so the current
> manual is for Lua 5.3, not for Lua 5.3.3. The next version (5.4) will
> have this new behaviour documented, provided we find a good way to
> explain it. Suggestions?

If pattern can match the empty string but always matches the longest
possible sequence, it will not match the empty string at a position
adjacent to the most recent match.

As an example, the following loop will pass all items, including empty ones,
of a comma-separated list to the function myfunc.

    for item in string.gmatch(list,"[^,]*") do
        myfunc(item)
    end