一个正则表达式引发的血案

14-03-01 banq
    

比较如下两个正则表达式:

(?:.*?(?:\\s|,)+)*no\\s+air\\s+conditioning.*?
(?:.*?(?:\\s|,)+)?no\\s+air\\s+conditioning.*?
<p>

区别仅仅在于*和?的区别,一字之差性能就相差很多:前者是35699.334;后者是108.686

完整测试代码:

String TEST_VALUE = "ABS, traction control, front and side airbags, Isofix child seat anchor points, no air conditioning, electric windows, \r\nelectrically operated door mirrors";
double start = System.nanoTime();
Pattern pattern = Pattern.compile("^(?:.*?(?:\\s|,)+)*no\\s+air\\s+conditioning.*$");
assertTrue(pattern.matcher(TEST_VALUE).matches());
double end = System.nanoTime();
LOGGER.info("Took {} micros", (end - start) / (1000 ));
<p>

两分钟以后这个程序还在运行,已经堵塞了CPU: