首页 > 代码库 > 正则不要乱用

正则不要乱用

正则不要乱用


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

 

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


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

 

String TEST_VALUE = "http://www.mamicode.com/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 ));

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

 

正则不要乱用