是否有正则表达式进行转换的方法:
M134.02,43.35c-12.62,1.4-29.25,6.59-39.85,19.65l15.35-5.82c26.24-18.1,54.45-10.65,62.99-0.11l1.27,1.34l0.02-0.04C169.6,49.83,155.11,41.01,134.02,43.35
进入:
["M", 134.02, 43.35],
["c", -12.62, 1.4, -29.25, 6.59, -39.85, 19.65],
["l", 15.35, -5.82]
// and so on...
我目前设置了这个正则表达式:
([a-zA-Z])(-?(\d+(\.\d+)?),?)+
但是,替换为:
["$1", $2]\n
仅取最后一位数字,结果为:
["M", 43.35],
["c", 19.65],
["l", -5.82],
["c", -0.11],
["l", 1.34],
["l", -0.04],
["C", 43.35],
好吧,我不需要在一个正则表达式中执行此操作(但是,首选),我只是期待利用IDE的Find & Replace + Regex
将http://readysetraphael.com/生成的字符串路径转换为数组路径。
我的IDE是phpStorm,所以我想它需要Java友好的正则表达式模式。我不是Java开发人员,所以我不知道我有什么额外的选择。
答案 0 :(得分:3)
也许你不需要这样一个特定的正则表达式。尝试
String s = "M134.02,43.35c-12.62,1.4-29.25,6.59-39.85,19.65l15.35-5.82c26.24-18.1,54.45-10.65,62.99-0.11l1.27,1.34l0.02-0.04C169.6,49.83,155.11,41.01,134.02,43.35";
s = s.replaceAll("(\\d)-", "$1,-").replaceAll("([a-zA-Z])([^a-zA-Z]+)", "[\"$1\", $2]\n");
System.out.println(s);
打印
["M", 134.02,43.35]
["c", -12.62,1.4,-29.25,6.59,-39.85,19.65]
["l", 15.35,-5.82]
["c", 26.24,-18.1,54.45,-10.65,62.99,-0.11]
["l", 1.27,1.34]
["l", 0.02,-0.04]
["C", 169.6,49.83,155.11,41.01,134.02,43.35]
答案 1 :(得分:0)
你几乎得到了它。只需将第二个术语包装在一个额外的大括号中即可捕获所有数字
([a-zA-Z])((?:-?(?:\d+(?:\.\d+)?),?)+)
我将其他(...)
更改为(?:...)
,以避免不必要地捕获子表达式。
捕获群组的测试用例:
public class CaptureTest {
public static void main(String[] args) {
String s = "M134.02,43.35c-12.62,1.4-29.25,6.59-39.85,19.65l15.35-5.82c26.24-18.1,54.45-10.65,62.99-0.11l1.27,1.34l0.02-0.04C169.6,49.83,155.11,41.01,134.02,43.35";
String t = s.replaceAll("([a-zA-Z])((?:-?(?:\\d+(?:\\.\\d+)?),?)+)", "[\"$1\", $2],");
System.out.println(s);
System.out.println(t);
}
}
和输出
M134.02,43.35c-12.62,1.4-29.25,6.59-39.85,19.65l15.35-5.82c26.24-18.1,54.45-10.65,62.99-0.11l1.27,1.34l0.02-0.04C169.6,49.83,155.11,41.01,134.02,43.35
["M", 134.02,43.35],["c", -12.62,1.4-29.25,6.59-39.85,19.65],["l", 15.35-5.82],["c", 26.24-18.1,54.45-10.65,62.99-0.11],["l", 1.27,1.34],["l", 0.02-0.04],["C", 169.6,49.83,155.11,41.01,134.02,43.35],
这是java -version
java版“1.7.0_03”
OpenJDK运行时环境(IcedTea7 2.1.1pre)(7~u3-2.1.1~pre1-1ubuntu2)
OpenJDK 64位服务器VM(内置22.0-b10,混合模式)