获取与正则表达式匹配的每个字符串的数组

时间:2011-02-07 05:47:27

标签: java arrays regex

我如何解析这样的文件:

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

我能做到

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

但我怎样才能得到每一个结果?

1 个答案:

答案 0 :(得分:12)

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