【问题标题】:How to get multiple regex matches in Java?如何在 Java 中获得多个正则表达式匹配?
【发布时间】:2012-07-16 15:57:39
【问题描述】:

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

【问题讨论】:

    标签: java regex multiple-matches


    【解决方案1】:

    创建一个匹配器并使用find() 将其定位到下一个匹配项。

    【讨论】:

      【解决方案2】:

      这是一个代码示例:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-04-24
        • 2011-03-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-14
        相关资源
        最近更新 更多