Java: Find number of regex matches in a String

Here's how to count the number of matches for a regular expression in a string:

Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
int count = 0;
while (m.find())
    count++;

Comments

Be the first to comment!