1

有没有办法用正则表达式进行转换:
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],

好吧,我不需要在一个正则表达式中执行它(虽然,首选),我只是期待利用 IDEFind & Replace + Regexhttp://readysetraphael.com/生成的字符串路径转换为数组路径。

我的 IDE 是 phpStorm,所以我猜它需要 Java 友好的正则表达式模式。虽然我不是 Java 开发人员,所以我不知道我有什么额外的选择。

4

2 回答 2

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]
于 2012-11-28T09:59:36.793 回答
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 version "1.7.0_03"
OpenJDK Runtime Environment (IcedTea7 2.1.1pre) (7~u3-2.1.1~pre1-1ubuntu2)
OpenJDK 64-Bit Server VM (build 22.0-b10,混合模式)

于 2012-11-28T10:03:58.663 回答