lua-users home
lua-l archive

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


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"
[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.