1

I have a URL pattern that looks like this: ^foo\/(?P<prefix>[a-z]+)\/bar\/$.

I need the sequence of characters in the middle to be different than another sequence of characters, let's say ab.

foo/somestring/bar/ and foo/abcde/bar/ should match, but foo/ab/bar/ should not.

I've come as far as ^foo\/(?P<prefix>(?!ab)[a-z]+)\/bar\/$, but that doesn't match foo/abcde/bar/.

0

1 Answer 1

1

You just forgot about restricting the negative lookahead value until the /:

^foo\/(?P<prefix>(?!ab\/)[a-z]+)\/bar\/$
                      ^^ 

See the regex demo

If you use (?!ab), it forbids matching values that start with ab. Adding the trailing boundary makes it disallow anything that equals that value inside the negative lookahead.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.