java正则表达式捕获组 [英] java regex capture group

查看:184
本文介绍了java正则表达式捕获组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to capture the right part after the : using java expr, but in the following code, the printed capture group is the whole string, what's wrong?

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));
    }
}


推荐答案

编号子组以1,0开头是全文。用你的循环直到数+ 1。

Numbering of subgroups starts with 1, 0 is the full text. Just go till count+1 with your loop.

这篇关于java正则表达式捕获组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆