简体   繁体   中英

Java Regex Negative Lookahead

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?

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

What you meant is probably \\bbar\\b (ie /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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM