否定的前瞻性正则表达式无效

时间:2011-03-23 22:49:44

标签: java regex regex-negation regex-lookarounds

input1="caused/VBN by/IN thyroid disorder"

要求:找到单词"caused",后跟斜杠,后跟任意数量的大写字母 - 而不是空格+ "by/IN

在上面的示例中,"caused/VBN"之后是" by/IN",因此'cause'不应该匹配。

input2="caused/VBN thyroid disorder" 

"by/IN"未跟踪原因,因此应匹配

regex="caused/[A-Z]+(?![\\s]+by/IN)"

caused/[A-Z]+ - 单词'引起'+ / +一个或多个大写字母
(?![\\s]+by) - 负向前瞻 - 不匹配空格和

以下是我用来测试的简单方法

public static void main(String[] args){
    String input = "caused/VBN by/IN thyroid disorder";

    String regex = "caused/[A-Z]+(?![\\s]+by/IN)";

    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(input);

    while(matcher.find()){
        System.out.println(matcher.group());
    }

输出:caused/VB

我不明白为什么我的负面前瞻正则表达式无效。

2 个答案:

答案 0 :(得分:7)

您需要在正则表达式中包含单词边界:

String regex = "caused/[A-Z]+\\b(?![\\s]+by/IN)";

没有它你可以得到一个匹配,但不是你期望的:

"caused/VBN by/IN thyroid disorder";
 ^^^^^^^^^
 this matches because "N by" doesn't match "[\\s]+by"

答案 1 :(得分:3)

将调整字符类[] +匹配(通过回溯),以便前瞻匹配。

你要做的是停止回溯,使表达式[] +完全匹配 这可以通过几种不同的方式完成。

  1. 积极向前看,然后是消费 "caused(?=(/[A-Z]+))\\1(?!\\s+by/IN)"

  2. 一个独立的子表达式
    "caused(?>/[A-Z]+)(?!\\s+by/IN)"

  3. 一个积极的量词 "caused/[A-Z]++(?!\\s+by/IN)"

相关问题