if (thirdLastLine.matches("Tests run: 12, Failures: 3")) {
System.out.println("Found: " + thirdLastLine);
} else {
System.out.println("No conditionString In File");
}
在if语句中写入值需要我为每个值写一个if条件,这是一个不好的做法。我想知道如何在没有任何价值的情况下做到这一点。例如:if(lines.contains("测试运行:'',失败:''"))尝试了这一点,但它没有工作..我该怎么办?感谢
答案 0 :(得分:4)
您可以使用regular expressions。
例如:
// your input String
String line = "Tests run: 12, Failures: 3";
// Defines a pattern where "Failures: [any number of digits > 0]"
Pattern p = Pattern.compile("Failures: [1-9]+[0-9]*");
// Defines a matcher for your pattern against that line
Matcher m = p.matcher(line);
// iterates over matches and prints the outcome if any
while (m.find()) {
System.out.printf("Found: %s%n", m.group());
}
<强>输出强>
Found: Failures: 3
答案 1 :(得分:0)
使用正则表达式:
if (lines.matches("Tests run: [0-9]+, Failures: [0-9]+")) {
System.out.println("OK");
}
else {
System.out.println("KO");
}
编辑:
我在正则表达式中将*
替换为+
。
答案 2 :(得分:0)
if(thirdLastLine.matches(“测试运行:([1-9] | 1 [0-9] | 2 [0-3]),失败:([1-9] | 1 [0-9] | 2 [0-3])“)){ 的System.out.println( “OK”); } 其他{ System.out.println(“不好”); }
这个正则表达式在服用了很长时间后成功了。谢谢你的回答:)