java中获取表达式匹配到的内容

来源:互联网 发布:影音嗅探专家 mac 编辑:程序博客网 时间:2024/06/06 18:52

matcher.find()表示是否在指定的字符串中有匹配到的内容;

如果有匹配到,则通过matcher.group(i)循环输出所有匹配到的内容。

matcher.groupCount()表示匹配到的内容个数。

代码示例如下:

String content = HttpClientUtil.getByUrl(singerUrl,charset);//<a href=\"\/artist\/104331\/songlist\">String regex = "\\/artist\\\\/[0-9]+\\\\\\/songlist";Pattern pattern = Pattern.compile(regex);Matcher matcher = pattern.matcher(content);if(matcher.find()){for(int i=0; i<=matcher.groupCount(); i++){System.out.println(i+":"+matcher.group(i));}}


0 0
原创粉丝点击