简体   繁体   English

java负面lookbehind正则表达式bug?

[英]java negative lookbehind regex bug?

My java @windows is: 我的java @windows是:

java version "1.6.0_29"
Java(TM) SE Runtime Environment (build 1.6.0_29-b11)
Java HotSpot(TM) Client VM (build 20.4-b02, mixed mode, sharing)

Code

Pattern.compile(".+(?<!(xxx|idea|perforator|pycharm|s).*)").matcher("xxx").matches() //returns TRUE while it should return FALSE
Pattern.compile(".+(?<!(xxx|idea|perforator|pycharm|s).*)").matcher("perforator").matches() //returns FALSE

Looks like negative lookbehind fails if string is smaller than 8 chars. 如果字符串小于8个字符,则看起来负面的lookbehind会失败。

Is it bug or do I misunderstand something about regex? 是错误还是我误解了正则表达式?

Lookbehinds in Java cannot have variable-length things like .* , only things like alternations and finite repetitions. Java中的Lookbehinds不能有像.*这样的可变长度的东西,只有像交替和有限重复这样的东西。

More Information: http://www.regular-expressions.info/lookaround.html#limitbehind 更多信息: http//www.regular-expressions.info/lookaround.html#limitbehind

If you need to match substrings that do not contain some words (as a part of a bigger expression) you can use: 如果您需要匹配不包含某些单词的子串(作为更大表达式的一部分),您可以使用:

(?s:(?!xxx|idea|perforator|pycharm|s).)*

If that is the only thing you are doing, just invert the result of the match: 如果这是您唯一要做的事情,只需反转匹配的结果:

xxx|idea|perforator|pycharm|s

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

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