Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting an array of every string that matches a Regular expression

Tags:

java

arrays

regex

How would I parse a file like this:

Item costs $15 and is made up of --Metal--
Item costs $64 and is made up of --Plastic--

I can do

Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
String result = m.group();

But how would I get EVERY result?

like image 515
Joshua Coffey Avatar asked Feb 07 '11 05:02

Joshua Coffey


1 Answers

Pattern p = Pattern.compile(regex); 
Matcher m = p.matcher(input);
List<String> matches = new ArrayList<String>();
while(m.find()){
    matches.add(m.group());
}
like image 107
Nishant Avatar answered Sep 22 '22 10:09

Nishant