java获取正则表达式重叠匹配

时间:2013-06-03 11:57:54

标签: java regex

如何获得另一场比赛中包含的正则表达式匹配?

我试图在同一个句子中匹配一个人的名字,然后是一个城市。所以我这样做:

String regex="(Bob|Mary)\\b[^\\.\\?!]*?\\b(Paris|London)\\b.*?[\\.\\?!]";
Pattern pattern=Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
Matcher matcher=pattern.matcher("Bob and Mary are planning to go to Paris. They want to leave before July.");

这将匹配“鲍勃和玛丽计划去巴黎。”,这是正确的。但它并不匹配“玛丽计划去巴黎。”,这实际上是我提到的第一场比赛的一部分。如何从“玛丽”开始进行第二次子赛事?

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

结果:

Bob and Mary are planning to go to Paris.

这是正确的。但我希望输出如下:

Bob and Mary are planning to go to Paris.
Mary are planning to go to Paris.

2 个答案:

答案 0 :(得分:1)

这是你想要做的吗?

String regex = "(?=((Bob|Mary)\\b[^\\.\\?!]*?\\b(Paris|London)\\b.*?[\\.\\?!]))";
Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern
        .matcher("Bob and Mary are planning to go to Paris. They want to leave before July.");
while (matcher.find()){
    System.out.println(matcher.group(1));
}

输出:

Bob and Mary are planning to go to Paris.
Mary are planning to go to Paris.

正常情况下,正则表达式会消耗它匹配的内容,因此在下一场比赛中不可能使用相同的字符串部分。要解决此问题,我们可以使用look-ahead机制(?=...)groups

答案 1 :(得分:1)

您也可以尝试使用这样的正则表达式:

String s = "Bob and Mary are planning to go to Paris. They want to leave before July.";
        Pattern p = Pattern.compile("(Bob|Mary).*Paris");
        Matcher m = p.matcher(s);
        int i = 0;
        while(m.find(i)) { // set start index for "find"
            System.out.println(m.group());
            i = m.start() + 1; // update start index to start from beginning of last match + 1
        }
    }

O / P:

Bob and Mary are planning to go to Paris
Mary are planning to go to Paris
相关问题