[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: gsub bug? 2 results from anchored gsub
- From: Sean Conner <sean@...>
- Date: Mon, 27 Jul 2015 03:28:19 -0400
It was thus said that the Great Daurnimator once stated:
> I want to ensure that a string always ends in a single "/".
> If it has more than one, the extras should be removed
> If it has none, a "/" should be appended.
> "/*$" should match all the '/' at the end of the string, and replace
> them with a single "/".
> I got an unexpected result:
> > ("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"
, which is zero or more '/', and replaces it with a slash. It's working
-spc (You may have to switch to LPEG ... )
 Thus sayeth the Master Control Program.