Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何优化java模式替换_Java_Regex - Fatal编程技术网

如何优化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.")