lua-users home
lua-l archive

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


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?
It seems odd to me that you could get 2 replacements for an anchored match.
Though as far as I can see, a strict reading of the manual doesn't disallow it.


Daurn.