我有这样的模式:Java的模式与团体
Pattern p = Pattern.compile("([0-9]){11}");
我的问题是如何从集团获得所有数字和概括。 我与此代码尝试,但不工作:
Matcher matcher = p.matcher(field);
int result = 0;
if (matcher.matches()) {
for (int i = 0; i <= matcher.groupCount(); i++) {
String number = matcher.group(i + 1);
result += Integer.parseInt(number);
}
return result;
} else {
return -1;
}
它找到的唯一的组是最后一组。
这是因为'.matches()'只有每输入匹配了一次。 – 2013-02-08 21:58:45
和['Matcher#groupCount()'](http://docs.oracle.com/javase/7/docs/api/java/util/regex/Matcher.html#groupCount())返回捕获组的数量在匹配者的**模式**中。 – jlordo 2013-02-08 21:59:49
你是想总结个人的单个数字吗?!? ''1234“= 1 + 2 + 3 + 4' – 2013-02-08 22:04:30