java.util.regex.Matcher confused group
问题 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