如何在Java中获取多个正则表达式匹配?

13

我怎样在Java中查找所有与正则表达式匹配的子字符串?(类似于.Net中的Regex.Matches

2个回答

18

这里是一个代码示例:

int countMatches(Pattern pattern, String str) {
  int matches = 0;
  Matcher matcher = pattern.matcher(str);
  while (matcher.find())
    matches++;
  return matches;
}

17

创建一个 Matcher,并使用 find() 在下一个匹配位置。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接