[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Anchors in string.gmatch()
- From: Dirk Laurie <dirk.laurie@...>
- Date: Mon, 20 Aug 2012 05:47:48 +0200
2012/8/20 Egor Skriptunoff <egor.skriptunoff@gmail.com>:
> Lua manual says:
>> For this function, a caret '^' at the start of a pattern
>> does not work as an anchor, as this would prevent
>> the iteration.
>
> Amazingly, $ still works as an anchor!
> Why is it so asymmetric?
Because the string is processed left-to-right. That introduces
a fundamental asymmetry.
> Does $ prevent the iteration, therefore must be disabled also?
No. $ simply says that a match of only part of the string is
not good enough.
> It is funny that one can get more than 1 match
> with $-anchored pattern:
> for a in ('Lua'):gmatch'.*$' do print(a) end
> for a in ('Lua'):gmatch'().*$' do print(a) end
The empty string matches ".*". You need ".+" to prevent that.
(I have lost count of how many times I have spent 30
minutes on debugging because I forgot this.)