Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JDK1.8的java.util.regex.Pattern中的Bug?_Java_Regex - Fatal编程技术网

JDK1.8的java.util.regex.Pattern中的Bug?

JDK1.8的java.util.regex.Pattern中的Bug?,java,regex,Java,Regex,我意外地想出了以下程序,它永远运行,而不是打印出一个真/假结果。那是虫子吗?我承认,由于匹配算法的贪婪/不情愿/占有性,运行时间可能太长,而不是无限长。无论如何,我想引起大家的注意 String str = "[EXCEPTION] org.jfree.chart.annotations.junit.CategoryTextAnnotationTests.testEquals(org.jfree.chart.annotations.junit.CategoryTextAnnotationTest

我意外地想出了以下程序,它永远运行,而不是打印出一个真/假结果。那是虫子吗?我承认,由于匹配算法的贪婪/不情愿/占有性,运行时间可能太长,而不是无限长。无论如何,我想引起大家的注意

String str = "[EXCEPTION] org.jfree.chart.annotations.junit.CategoryTextAnnotationTests.testEquals(org.jfree.chart.annotations.junit.CategoryTextAnnotationTests) false java.lang.NullPointerException [STACKTRACE] org.jfree.chart.annotations.AbstractAnnotation.notifyListeners(AbstractAnnotation.java:145) org.jfree.chart.annotations.AbstractAnnotation.fireAnnotationChanged(AbstractAnnotation.java:129) org.jfree.chart.annotations.CategoryTextAnnotation.setCategoryAnchor(CategoryTextAnnotation.java:157) org.jfree.chart.annotations.junit.CategoryTextAnnotationTests.testEquals(CategoryTextAnnotationTests.java:101) sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) java.lang.reflect.Method.invoke(Method.java:606) junit.framework.TestCase.runTest(TestCase.java:176) junit.framework.TestCase.runBare(TestCase.java:141) junit.framework.TestResult$1.protect(TestResult.java:122) junit.framework.TestResult.runProtected(TestResult.java:142) junit.framework.TestResult.run(TestResult.java:125) junit.framework.TestCase.run(TestCase.java:129) junit.framework.TestSuite.runTest(TestSuite.java:252) junit.framework.TestSuite.run(TestSuite.java:247) org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:86) org.pitest.junit.adapter.CustomRunnerExecutor.run(CustomRunnerExecutor.java:42) org.pitest.junit.adapter.AdaptedJUnitTestUnit.execute(AdaptedJUnitTestUnit.java:85) org.pitest.mutationtest.execute.MutationTimeoutDecorator$1.run(MutationTimeoutDecorator.java:88) java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) java.util.concurrent.FutureTask.run(FutureTask.java:262) java.lang.Thread.run(Thread.java:745)\nsdas";
System.out.println(str.matches("\\[EXCEPTION\\] (.)* \\[STACKTRACE\\]( (.)*\\((.)*\\))*"));
最后但并非最不重要的一点是,请注意前缀\nsdas会触发此行为

那是虫子吗

我不这么认为。我认为这只是一个由设计拙劣的正则表达式引起的指数回溯的例子

问题在于这种模式

  ( (.)*\\((.)*\\))*   

具体地说,就是这个问题。它们将匹配任何字符序列,包括和“”。如果您将它们更改为[^]*您应该会看到更好的性能。

请注意,String::matches是根据java.util.regex.Pattern实现的。您的正则表达式确实可以工作,但由于正则表达式可以匹配给定字符串的方式有很多,因此出现了所谓的灾难性回溯。你的正则表达式完成的时间太长了;您可以通过将str的值减半来检查这一点。但是,字符串不是问题,而是正则表达式。@Aominè,感谢您引入灾难性回溯的概念。我从你那里学到了一些东西: