Reputation: 83279
I'm trying to match some paths, but not others via regexp. I want to match anything that starts with "/profile/" that is NOT one of the following:
Here is the regex I'm trying to use that doesn't seem to be working:
^/profile/(?!attributes|essays|edit)$
For example, none of these URLs are properly matching the above:
Upvotes: 2
Views: 246
Reputation: 1469
comments are hard to read code in, so here is my answer in nice format
def mpath(path, ignore_str = 'attributes|essays|edit',anything = True):
any = ''
if anything:
any = '.*?'
m = re.compile("^/profile/(?!(?:%s)%s($|/)).*$" % (ignore_str,any) )
match = m.search(path)
if match:
return match.group(0)
else:
return ''
Upvotes: 1
Reputation: 838156
You need to say that there can be any characters until the end of the string:
^/profile/(?!attributes|essays|edit).*$
Removing the end-of-string anchor would also work:
^/profile/(?!attributes|essays|edit)
And you may want to be more restrictive in your negative lookahead to avoid excluding /profile/editor
:
^/profile/(?!(?:attributes|essays|edit)$)
Upvotes: 4