简体   繁体   English

如何优化java模式替换

[英]How to optimise java pattern replace

How to optimise below regex as I have to run it over billion records如何优化下面的正则表达式,因为我必须运行超过 10 亿条记录

String test = "source[{\"name\": \"Mokole\", \"country\": \"CD\",\"location\": {\"lat\": .033333, \"lon\": -.583333}}]}\n";

String result = test.replace(" ."," 0.").replace("-.","-0.");
String result = test.replaceAll("([ -])\\.","$1\\0.")

您可以将2表达式组合成1

as I have to run it over billion records ==> Then DON'T use replaceAll() which creates the pattern each time you call it.因为我必须运行超过 10 亿条记录==> 然后不要使用每次调用它时都会创建模式的replaceAll()

Create a static Pattern using the same regex String using Pattern.compile .使用Pattern.compile使用相同的正则表达式字符串创建static Pattern Then for each input String call pattern.matcher(inputString) .然后对于每个输入字符串调用pattern.matcher(inputString) Then call matcher.replaceAll() method.然后调用matcher.replaceAll()方法。

PS : Use regex mentioned by VKS in his answer PS:使用VKS在他的回答中提到的正则表达式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM