我编写了以下正则表达式,因此该正则表达式将与输入不匹配;它正在工作,但循环运行无限时间。如何解决问题
String originalRegex ="(?s)\\00|\\+ADw-|\\+AD4-|%[0-9a-f]{2}|System[.][a-z]|javascript\\s*:|>(?:\".*|^'.*|[^a-z]'.*|'[^a-z].*|')[-+\\*/%=&|^~\"']|\\?.*<:|\\(\\s*[a-z]{2,}\\.[a-z]{2,}.*\\)";
String xmlData = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><configuration xmlns=\"http://www.example.com/api/2.2\" timestamp=\"str111\" version=\"2.2\"><domain account=\"4af17966-c841-4b97-a94a-edd7a0769\" /></configuration>";
String freetext = ">(?:\".*|^'.*|[^a-z]'.*|'[^a-z].*|')[-+\\*/%=&|^~\"']|\\?.*<:";
final Pattern PATTERN_1 = Pattern.compile(freetext);
Matcher matcher = PATTERN_1.matcher(xmlData);
while (!matcher.find()) {
System.out.println("Good Job");
}
最佳答案
Java 的 .find()
方法返回“当且仅当输入序列的子序列与此匹配器的模式匹配时才返回 true” - see the documentation 。在您的代码中,如果没有找到匹配项,则:
while (!matcher.find()) {
System.out.println("Good Job");
}
评估为:
while (!false) {
System.out.println("Good Job");
}
或者更简单:
while (true) {
System.out.println("Good Job");
}
因此,你的无限循环。
关于java - 正则表达式检查运行无限时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18442437/