如何优化java模式替换
如何优化下面的正则表达式,因为我必须运行它超过十亿记录如何优化java模式替换,java,regex,Java,Regex,如何优化下面的正则表达式,因为我必须运行它超过十亿记录 String test = "source[{\"name\": \"Mokole\", \"country\": \"CD\",\"location\": {\"lat\": .033333, \"lon\": -.583333}}]}\n"; String result = test.replace(" ."," 0.").replace("-.","-0."); 您可以将2正则表达式组合到1中,因为我必须在十亿条记录上运行它==>
String test = "source[{\"name\": \"Mokole\", \"country\": \"CD\",\"location\": {\"lat\": .033333, \"lon\": -.583333}}]}\n";
String result = test.replace(" ."," 0.").replace("-.","-0.");
您可以将
2
正则表达式组合到1
中,因为我必须在十亿条记录上运行它==>然后不要使用replaceAll()
,每次调用它时都会创建模式
使用模式使用相同的正则表达式字符串创建静态模式
。编译
。然后为每个输入字符串调用pattern.matcher(inputString)
。然后调用matcher.replaceAll()
方法
注:使用VKS在他的回答中提到的正则表达式将不起作用。将用于匹配till而不是unicode。结果类似于{“name”:{}0.Mokole“,{0.country”:{0.CD”,“location”:{}0.lat”:{0.033333,{0.lon”:{0..583333}}]}@rohit——正则表达式看起来很好。但是这种方法对于扫描数十亿条记录仍然是低效的,当我使用静态模式时,我也能看到差异。谢谢
String result = test.replaceAll("([ -])\\.","$1\\0.")