12

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

4

2 回答 2

16

创建一个 Matcher 并用于find()将其定位在下一场比赛中。

于 2009-08-12T15:35:11.053 回答
16

这是一个代码示例:

int countMatches(Pattern pattern, String str) {
  int matches = 0;
  Matcher matcher = pattern.matcher(str);
  while (matcher.find())
    matches++;
  return matches;
}
于 2009-08-12T15:59:58.600 回答