Java:获取与Regular表达式匹配的每个字符串的数组 [英] Java: Getting an array of every string that matches a Regular expression
本文介绍了Java:获取与Regular表达式匹配的每个字符串的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何解析这样的文件:
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--
我可以做
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
String result = m.group();
但我怎么能得到每一个结果?
But how would I get EVERY result?
推荐答案
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
List<String> matches = new ArrayList<String>();
while(m.find()){
matches.add(m.group());
}
这篇关于Java:获取与Regular表达式匹配的每个字符串的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文