[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: gsub bug? 2 results from anchored gsub
- From: Tim Hill <drtimhill@...>
- Date: Mon, 27 Jul 2015 01:52:13 -0700
> On Jul 27, 2015, at 12:28 AM, Sean Conner <sean@conman.org> wrote:
>
>>
>>> ("d//"):gsub("/*$", "/")
>> d// 2
>>
>> This result suggests that there is an empty string being matched
>> between the last "/" and the end of the string.
>> It's matching the // and replacing that with "/"; but then it gets
>> confused and matches the empty string at the end, and ends up
>> inserting an extra /
>> Using 'print' as the match confirms:
>>
>>> ("d//"):gsub("/*$", print)
>> //
>>
>> d// 2
>>
>> Is this a bug in string.gsub?
>
> No. You're telling Lua that you want to match zero or more '/' followed
> by the end of string. Going through "d//", it first finds "//", which is
> zero or more '/', and replaces it with a slash. It then finds "END OF LINE"
> [1], which is zero or more '/', and replaces it with a slash. It's working
> as intended.
>
> -spc (You may have to switch to LPEG ... )
>
> [1] Thus sayeth the Master Control Program.
>
Hmm .. my vote goes with the OP. Matches are greedy so the first match should be on “//“ AND the end of the string. I found this interesting:
(“d//“):gsub(“/+$”, “/“)
d/ 1
—Tim