1 回答
TA贡献1801条经验 获得超16个赞
我猜想类似的表达:
(?::\[\[)?\[([-+]?\d+\.\d+)\s*,\s*([-+]?\d+\.\d+)\]\s*,?(?:\]\]}})?
使用$1
and $2
as lat
and进行适当的替换lon
,例如:
{\n"@type": "point",\n"lat":"$1",\n"lon":"$2"\n},\n
可能在某种程度上有效,但不完全有效。
演示
测试
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegularExpression{
public static void main(String[] args){
final String regex = "(?::\\[\\[)?\\[([-+]?\\d+\\.\\d+)\\s*,\\s*([-+]?\\d+\\.\\d+)\\]\\s*,?(?:\\]\\]\\}})?";
final String string = ":[[[7.365046,46.948655],[7.365046,46.949254],[7.367558,46.949254],[7.367558,46.948655],[7.365046,46.948655]]]}}";
final String subst = "{\"@type\": \"point\",\"lat\":\"$1\",\"lon\":\"$2\"},";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
final String result = matcher.replaceAll(subst);
System.out.println(result);
}
}
输出
{"@type": "点","lat":"7.365046","lon":"46.948655"},{"@type": "点","lat":"7.365046","lon":" 46.949254"},{"@type": "点","lat":"7.367558","lon":"46.949254"},{"@type": "点","lat":"7.367558","经度":"46.948655"},{"@type": "点","纬度":"7.365046","经度":"46.948655"},
正则表达式电路
jex.im可视化正则表达式:
添加回答
举报