[Answered ]-Regex to match url with postfix?

2👍

You can either limit the scope of the second regex by adding a $ to the end, thus making the second regex only match urls that have two directories in its path, or you can add a negative lookahead to the end of the second regex to search only for urls that do not have postfix as the third directory:

^(/(?P<required_part>[a-zA-Z0-9\-]+))(/(?P<optional_part>[a-zA-Z0-9\-]+))?(?!/postfix)

I would also suggest adding a ^ to the start of your regexes. Without it the above regex can backtrack and match /foo/bar/postfix/baz.

Leave a comment