1 回答

TA貢獻1801條經(jīng)驗 獲得超16個贊
我猜想類似的表達:
(?::\[\[)?\[([-+]?\d+\.\d+)\s*,\s*([-+]?\d+\.\d+)\]\s*,?(?:\]\]}})?
使用$1
and?$2
as?lat
and進行適當(dāng)?shù)奶鎿Qlon
,例如:
{\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","經(jīng)度":"46.948655"},{"@type": "點","緯度":"7.365046","經(jīng)度":"46.948655"},
正則表達式電路
jex.im可視化正則表達式:
添加回答
舉報