开发者

Regexp matching except

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:

  • /profile/attributes
  • /profile/essays
  • /profile/edit

Here is the regex I'm trying to use that doesn't seem to be work开发者_运维问答ing:

^/profile/(?!attributes|essays|edit)$

For example, none of these URLs are properly matching the above:

  • /profile/matt
  • /profile/127
  • /profile/-591m!40v81,ma/asdf?foo=bar#page1


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)$)


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 ''
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜