java.util.regex.Matcher confused group

依然范特西╮ 提交于 2019-12-08 04:37:30

问题


I'm having trouble getting the right group of a regex match. My code boils down to following:

Pattern fileNamePattern = Pattern.compile("\\w+_\\w+_\\w+_(\\w+)_(\\d*_\\d*)\\.xml");
Matcher fileNameMatcher = fileNamePattern.matcher("test_test_test_test_20110101_0000.xml");

System.out.println(fileNameMatcher.groupCount());

if (fileNameMatcher.matches()) {
    for (int i = 0; i < fileNameMatcher.groupCount(); ++i) {
        System.out.println(fileNameMatcher.group(i));
    }
}

I expect the output to be:

2
test
20110101_0000

However its:

2
test_test_test_test_20110101_0000.xml
test

Does anyone have an explanation?


回答1:


Group(0) is the whole match, and group(1), group(2), ... are the sub-groups matched by the regular expression.
Why do you expect "test" to be contained in your groups? You didn't define a group to match test (your regex contains only the group \d*_\d*).




回答2:


Group 0 is the whole match. Real groups start with 1, i.e. you need this:

System.out.println(fileNameMatcher.group(i + 1)); 



回答3:


  • group(0) should be the entire match ("test_test_test_test_20110101_0000.xml");
  • group(1) should be the sole capture group in your regex ("20110101_0000").

This is what I am getting. I am puzzled as to why you'd be getting a different value for group(1).




回答4:


actually your for loop should INCLUDE groupCount() using "<=" :

for (int i = 0; i <= fileNameMatcher.groupCount(); ++i) {
    System.out.println(fileNameMatcher.group(i));
}

thus your output then will be:

2
test_test_test_test_20110101_0000.xml
test
20110101_0000

the groupCount() will not count group 0 matching the whole string.

first group will be "test" as matched by (\w+) and

second group will be "20110101_0000" as matched by (\d*_\d*)



来源:https://stackoverflow.com/questions/5716703/java-util-regex-matcher-confused-group

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!