Dave's Brain

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 }

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.