簡體   English   中英

java負面lookbehind正則表達式bug?

[英]java negative lookbehind regex bug?

我的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)

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

如果字符串小於8個字符,則看起來負面的lookbehind會失敗。

是錯誤還是我誤解了正則表達式?

Java中的Lookbehinds不能有像.*這樣的可變長度的東西,只有像交替和有限重復這樣的東西。

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

如果您需要匹配不包含某些單詞的子串(作為更大表達式的一部分),您可以使用:

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

如果這是您唯一要做的事情,只需反轉匹配的結果:

xxx|idea|perforator|pycharm|s

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM