找到Matcher找到匹配的捕获组

时间:2015-07-11 20:11:05

标签: java regex matcher

我有一个包含多个捕获组的正则表达式:

String regex = "(first|second|third)|(one|two|three)|(uno|dos|tres)";

我可以遍历每组的String发现模式:

String text = "one two uno third second tres";
Matcher matcher = Pattern.compile(regex).matcher(text);
for(int index = 0; matcher.find(index); index = matcher.end()) {
    System.out.println(matcher.group());
}

问题是,它并没有告诉我它来自哪个群体。

我可以将针对每个可用组matcher.group(#)找到的组进行比较,然后选择不返回null的任何人:

int numOfGroups = 3;
for(int index = 0; matcher.find(index); index = matcher.end()) {
    String result = null;
    int group = 0;

    for(int i = 1; i <= numOfGroups; i++) {
        String tmp = matcher.group(i);
        if(tmp != null) {
            result = tmp;
            group = i;
            break;
        }
    }
    System.out.println(result + " " + group);
}

但是这会增加时间复杂度,每次迭代最多增加3个步骤(3组)。

我如何确定哪个组触发了匹配?

1 个答案:

答案 0 :(得分:0)

一组Matcher s,每个Pattern一个?您无法识别哪个组触发了匹配,但Matcher哪个匹配。

public static void main(String[] args) throws Exception {
    String text = "one two uno third second tres";
    Matcher[] matcher = { 
        Pattern.compile("(first|second|third)").matcher(text),
        Pattern.compile("(one|two|three)").matcher(text),
        Pattern.compile("(uno|dos|tres)").matcher(text)
    };

    for (int i = 0; i < matcher.length; i++) {
        while (matcher[i].find()) {
            System.out.println(matcher[i].group() + " " + i);
        }
    }
}

结果:

third 0
second 0
one 1
two 1
uno 2
tres 2