我有一个以下字符串:
String Str = "passed=20 failed=15 skipped=10";
从中我需要找到总和
-
passed
-
failed
-
skipped
使用正则表达式\d+
查找字符串中的所有数字,然后解析并求和它们:
String example = "passed=20 failed=15 skipped=10";
Pattern pattern = Pattern.compile("\d+");
Matcher matcher = pattern.matcher(example);
int sum = 0;
while (matcher.find()) {
sum += Integer.parseInt(matcher.group(0));
}
System.out.println(sum);