4

After looking through the answers that are already on StackOverflow regarding this issue, I settled with the most accurate one I could find:

Java regex: Negative lookahead

I went over to gskinner and tested it. I put /foo/(?!.*\\bbar\\b).+ in the pattern input box and the following in the regex match text area:

/foo/abc123doremi

/foo/abc123doremi/bar/def456fasola

Gskinner recognised both of these as matches though so clearly either Gskinner is wrong or the regex pattern above isn't correct. Any thoughts?

1 Answer 1

5

You are looking for \bbar\b while your text contains /bar/.

What you meant is probably \bbar\b (i.e. /foo/(?!.*\bbar\b).+)

Note that "duplicate the \" is only required inside of Java String literals. That makes writing regexs in Java a bit of a pain.

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.