Joshua Coffey
Joshua Coffey

Reputation: 330

Getting an array of every string that matches a Regular expression

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?

Upvotes: 8

Views: 11268

Answers (1)

Nishant
Nishant

Reputation: 55866

Pattern p = Pattern.compile(regex); 
Matcher m = p.matcher(input);
List<String> matches = new ArrayList<String>();
while(m.find()){
    matches.add(m.group());
}

Upvotes: 12

Related Questions