Currently, I have the following Python regex:
r'^https?://(www.)?domain.com/?(?P<path>.*)/?$'
That I’m replacing with:
r'/\g<path>/'
This works fine except in the scenario where the last character of the string is a slash (/). In that case, the .* greedily consumes the last /, so the subbed string ends up as /path//
Essentially, I’m stripping the domain from an absolute path, turning it into a relative path, and trying to ensure that the relative path both begins and ends with a /.
Any idea how I can exclude the last character from the match if and only if it’s a /? It seems I’ll probably need some sort of look-ahead, but I’m not sure exactly how to construct it.
Just make the asterisk lazy:
The
$at the end ensures that the entire string will be matched, and a trailing slash, if present, will always be matched by the/?.