I have this simple regular expression substitution just to ensure that some URL ends in a slash character:
url = re.sub("/*$", "/", "foo/")
...but it just happens that when I run that code, the result is unexpectedly foo//
.
After further experimentation the explanation I have found is that the regular expression /*$
matches the slash at the end of the string and replaces it by another slash, but then it matches again the empty string just after the replaced slash.
Is there any simple way to workaround this issue?
Update: Well, It seems you can tell sub
how many times you want the replacement done at most:
url = re.sub("/*$", "/", "foo/", 1)