How can I find all substrings that match a regex in Java? (Similar to Regex.Matches in .Net)
Asked
Active
Viewed 1.9k times
2 Answers
18
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;
}

ripper234
- 222,824
- 274
- 634
- 905
17
Create a Matcher and use find()
to position it on the next match.

Aaron Digulla
- 321,842
- 108
- 597
- 820