Skip to content Skip to sidebar Skip to footer

Match Trailing Slash With Python Regex

I try to match a trailing / like this: type(re.match('/$', u'https://x.x.x/')) However, this one does match: type(re.match('.*/$', u'https://x.x.x/')) <

Solution 1:

re.match search your pattern from beginning of you string. Since your string doesn't start with a '/', re.match('/$', u'https://x.x.x/') returns nothing.

You need to use re.search('/$', u'https://x.x.x/') to find your last slash.

Post a Comment for "Match Trailing Slash With Python Regex"