0

如何获得包含在另一个匹配项中的正则表达式匹配项?

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

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.
4

2 回答 2

3

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

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
        }
    }

开/关:

Bob and Mary are planning to go to Paris
Mary are planning to go to Paris
于 2016-10-18T06:08:30.653 回答
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.

通常正则表达式会消耗一次匹配的内容,因此在下一次匹配中不可能使用字符串的相同部分。为了摆脱这个问题,我们可以使用前瞻机制(?=...)

于 2013-06-03T12:09:58.027 回答