You need a regex that captures the 2 elements you need : text and value, then print them in the good order :
String line = "Spec Files: 15 passed, 5 failed, 20 total (100% completed) in 00:08:53";
Pattern p = Pattern.compile("(\\d+)\\s+(\\w+)");
Matcher m = p.matcher(line);
while (m.find()) {
System.out.println(m.group(2) + ": " + m.group(1));
}
/*
passed: 15
failed: 5
total: 20
Pass %:
Map<String, Integer> map = new HashMap<>();
while (m.find()) {
System.out.println(m.group(2) + ": " + m.group(1));
map.put(m.group(2), Integer.parseInt(m.group(1)));
}
double passPercentage = map.get("passed") / (double) map.get("total");
System.out.println(passPercentage);
OR
int passed = 0, total = 0;
while (m.find()) {
System.out.println(m.group(2) + ": " + m.group(1));
if (m.group(2).equals("passed")) {
passed += Integer.parseInt(m.group(1));
} else if (m.group(2).equals("total")) {
total += Integer.parseInt(m.group(1));
}
}
double passPercentage = passed / (double) total;
System.out.println(passPercentage);