简体   繁体   中英

How to optimise java pattern replace

How to optimise below regex as I have to run it over billion records

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.

Create a static Pattern using the same regex String using Pattern.compile . Then for each input String call pattern.matcher(inputString) . Then call matcher.replaceAll() method.

PS : Use regex mentioned by VKS in his answer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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