Browse - Programming Tips - How can I grab all occurances of a pattern in a string?

Date: 2012sep21
Language: Java


Q.  How can I grab all occurances of a pattern in a string?

A.  Do group(1) while find() gets hits.  Like this:

// This pattern will find all Capitalized words Pattern pat = Pattern.compile("([A-Z]\w+)"); Matcher matcher = pat.matcher(my_subject_string); while (matcher.find()) { String hit_word = matcher.group(1); // Do some with with the word }