public String group(int group) {
if (first < 0)
throw new IllegalStateException("No match found");
if (group < 0 || group > groupCount())
throw new IndexOutOfBoundsException("No group " + group);
if ((groups[group*2] == -1) || (groups[group*2+1] == -1))
return null;
return getSubSequence(groups[group * 2], groups[group * 2 + 1]).toString();
}
public static void main(String[] args) {
String content = "112233asdf55667788dsfsdf";
Pattern pattern = Pattern.compile("(\\d\\d)(\\d\\d\\d)(\\d)");
Matcher matcher = pattern.matcher(content);
while (matcher.find()){
System.out.println(matcher.group(0));
System.out.println(matcher.group(1));
System.out.println(matcher.group(2));
System.out.println(matcher.group(3));
}
}
group(0)表示没有分组,group(1),group(2)...group(n)对字符串进行分组,groups[group*2]表示下一次出现的index。(这里的分组1,2,3对应上面的三组括号,如果增加4,则数组越界groups[-1]报错)