if (thirdLastLine.matches("Tests run: ([1-9]|1[0-9]|2[0-3]), Failures: ([1-9]|1[0-9]|2[0-3])")) {
System.out.println("first choice");
String[] splitedString = thirdLastLine.split("\\s");
// System.out.println("splitedString: " + splitedString);
System.out.println("splitedString: " + Arrays.toString(splitedString));
int lastIndexofArray = (splitedString.length) - 1;
System.out.println("lastIndexOfArray: " + lastIndexofArray);
//for the test run:
int thirdIndexofArray = (splitedString.length) - 4;
System.out.println("thirdIndexofArray: " + thirdIndexofArray);
int returnedInteger = Integer.parseInt(splitedString[lastIndexofArray]);
int returnedThirdInteger = Integer.parseInt(splitedString[thirdIndexofArray]);
System.out.println("You failed: " + returnedInteger + " " + "test");
System.out.println("Numbers of test: " + returnedThirdInteger + "test");
feedbackString = "You failed: " + returnedInteger + " " + "test";
} else {
System.out.println("Nothing found");
}
thirdLastLine
包含字符串:
测试运行:12,失败:4
在txt文件中。失败位工作正常但似乎无法使测试运行位工作,因为该字符。我无法阅读'测试运行旁边的值:'因为它旁边有一个逗号,我的正则表达式不满足要求。有关如何解决此问题的任何建议吗?
答案 0 :(得分:2)
如果我理解了您的问题,建议您使用Pattern
获取Matcher
,然后使用Formatter
syntax之类的内容
Pattern p = Pattern.compile("Tests run: (\\d+), Failures: (\\d+)");
String thirdLastLine = "Tests run: 12, Failures: 4";
Matcher m = p.matcher(thirdLastLine);
if (m.matches()) {
int testCount = Integer.parseInt(m.group(1));
int failureCount = Integer.parseInt(m.group(2));
System.out.printf("Numbers of tests: %d tests%n", testCount);
String feedbackString = String.format("You failed: %d tests",
failureCount);
System.out.println(feedbackString);
} else {
System.out.println("Nothing found");
}
输出
Numbers of tests: 12 tests
You failed: 4 tests
答案 1 :(得分:0)
第一个数字后面有一个逗号:
"Tests run: ([1-9]|1[0-9]|2[0-3]), Failures: ([1-9]|1[0-9]|2[0-3])"
# here __^
在“失败”之前可能有未知数量的空格(或制表符),所以:
"Tests run:\s+([1-9]|1[0-9]|2[0-3]),\s+Failures:\s+([1-9]|1[0-9]|2[0-3])"
答案 2 :(得分:0)
您可以使用以下正则表达式匹配数字表达式
2[0-3]|1\d|\d
这将匹配0到23
如果你把它放在所有匹配模式中并考虑到JE SUIS CHARLIE的答案,
它变成了"Tests run: (2[0-3]|1\d|\d), Failures: (2[0-3]|1\d|\d)"
答案 3 :(得分:0)
你需要一个模式和匹配器来解决这些群体。
import java.util.regex.Matcher;
import java.util.regex.Pattern;
然后你可以检查匹配并提取匹配的组。
Pattern p = Pattern.compile("Tests run: ([0-9]+), Failures: ([0-9]+)");
Matcher m = p.matcher(thirdLastLine);
if(m.matches()) {
String testsRun = m.group(1);
String failedTests = m.group(2);
System.out.println("You failed: " + failedTests + " " + "tests");
System.out.println("Numbers of tests: " + testsRun + "tests");
} else {
System.out.println("Nothing found");
}
当然,您可能希望将结果转换为整数。
答案 4 :(得分:0)
Pattern p = Pattern.compile("Tests run: ([1-9]|1[0-9]|2[0-3]), Failures: ([1-9]|1[0-9]|2[0-3])");
Matcher m = p.matcher(thirdLastLine);
if (m.matches()) {
String testsRun = m.group(1);
String failedTests = m.group(2);
int testsRunsOutput = Integer.parseInt(testsRun);
int failedTestsOutput = Integer.parseInt(failedTests);
System.out.println("You failed: " + failedTestsOutput + " " + "tests");
System.out.println("Numbers of tests: " + testsRunsOutput + "tests");
System.out.println("Numbers of tests: " + testsRunsOutput + " " + "You failed: " + failedTestsOutput + " " + "tests");
feedbackString = "Numbers of tests: " + testsRunsOutput + " " + "You failed: " + failedTestsOutput + " " + "tests";
} else {
System.out.println("Nothing found");
}
我刚刚使用了同样的正则表达式作为我的问题和来自@Wil Selwood的代码。 当我使用'Tests run:12,Failures:4'
运行thirdLastLine时输出: 测试次数:12你失败了:4次测试