How to get multiple regex matches in Java?

牧云@^-^@ 提交于 2020-01-19 13:58:05

问题


How can I find all substrings that match a regex in Java? (Similar to Regex.Matches in .Net)


回答1:


Create a Matcher and use find() to position it on the next match.




回答2:


Here is a code sample:

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


来源:https://stackoverflow.com/questions/1266943/how-to-get-multiple-regex-matches-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!