[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Comprehending string.gsub
- From: Peter Cawley <lua@...>
- Date: Fri, 13 Apr 2012 12:07:25 +0100
On Fri, Apr 13, 2012 at 11:54 AM, Robert Virding
<robert.virding@erlang-solutions.com> wrote:
> I am having a bit of trouble comprehending a call to string.gsub in the pm.lua file in thelua-5.2.0 tests. There is a line:
>
> assert(string.gsub("abc", "%w", "%1%0") == "aabbcc")
>
> which works. My problem is that I don't why the substring %1 works and from where it gets its value. As far as I can see there is no captured substring at all. Using %2 generates an error (as I think it should).
While I don't particularly like the behaviour, I can form a
pseudo-explanation as to what is going on. When repl is a table or
function, and the pattern specifies no captures, then the whole match
is used as the sole capture. One way of implementing this is to say
that if a pattern P specifies no captures, then it is treated as (P).
This effect then spills over to when repl is a string, causing %1 to
be the whole match.