java正则表达式捕获组

23 java regex

我试图捕获正确的部分:使用java expr,但在下面的代码中,打印的捕获组是整个字符串,有什么问题?

String s ="xyz: 123a-45";   
String patternStr="xyz:[ \\t]*([\\S ]+)";
Pattern p = Pattern.compile(patternStr);
Matcher m = p.matcher(s);
//System.err.println(s);
if(m.find()){
    int count = m.groupCount();
    System.out.println("group count is "+count);
    for(int i=0;i<count;i++){
        System.out.println(m.group(i));
    }
}
Run Code Online (Sandbox Code Playgroud)

yan*_*kee 29

子组的编号从1,0开始是全文.用你的循环直到数+ 1.

  • 更确切地说,将`for`循环条件更改为`i <= count`. (8认同)