How to get multiple regex matches in Java?

前端 未结 2 1644
深忆病人
深忆病人 2020-12-11 02:15

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

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-11 03:11

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

提交回复
热议问题