How to get multiple regex matches in Java?

前端 未结 2 1640
深忆病人
深忆病人 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;
    }
    
    0 讨论(0)
  • 2020-12-11 03:14

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

    0 讨论(0)
提交回复
热议问题