简体   繁体   English

Java正则表达式负提前

[英]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: 浏览完关于此问题的StackOverflow上的答案后,我找到了最准确的答案:

Java regex: Negative lookahead Java正则表达式:负向提前

I went over to gskinner and tested it. 我去了gskinner并进行了测试。 I put /foo/(?!.*\\\\bbar\\\\b).+ in the pattern input box and the following in the regex match text area: 我将/foo/(?!.*\\\\bbar\\\\b).+放在模式输入框中,并将以下内容放在正则表达式匹配文本区域中:

/foo/abc123doremi / foo / abc123doremi

/foo/abc123doremi/bar/def456fasola / 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. Gskinner认为这两个都是匹配项,尽管很明显Gskinner错误或上述正则表达式模式不正确。 Any thoughts? 有什么想法吗?

You are looking for \\bbar\\b while your text contains /bar/ . 您的文字包含/bar/您正在寻找\\bbar\\b

What you meant is probably \\bbar\\b (ie /foo/(?!.*\\bbar\\b).+ ) 您的意思可能是\\bbar\\b (即/foo/(?!.*\\bbar\\b).+

Note that "duplicate the \\ " is only required inside of Java String literals. 注意, 在Java String文字内部需要“复制\\ ”。 That makes writing regexs in Java a bit of a pain. 这使得用Java编写正则表达式有点麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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