[英]Regular expression replace preserving groups
Is there a way with regular expression to transform: 有没有一种方法可以转换正则表达式:
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
into: 成:
["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...
I have currently set up this regex: 我目前设置了这个正则表达式:
([a-zA-Z])(-?(\\d+(\\.\\d+)?),?)+
But, replacing it with: 但是,用以下代替:
["$1", $2]\\n
Takes only the last digit value, resulting in: 仅取最后一位数值,导致:
["M", 43.35],
["c", 19.65],
["l", -5.82],
["c", -0.11],
["l", 1.34],
["l", -0.04],
["C", 43.35],
Well, I do not need to do it in one regex (though, preferred), I am just looking forward to utilize IDE's Find & Replace + Regex
to transform http://readysetraphael.com/ generated string paths to Array paths. 好吧,我不需要在一个正则表达式中做(虽然,首选),我只是期待利用IDE的
Find & Replace + Regex
将http://readysetraphael.com/生成的字符串路径转换为数组路径。
My IDE is phpStorm, so I guess it takes Java friendly regex patterns. 我的IDE是phpStorm,所以我想它需要Java友好的正则表达式模式。 I'm not Java developer though, so I do not know what extra options I have there.
我不是Java开发人员,所以我不知道我有什么额外的选择。
Perhaps you don't need such a specific regex. 也许你不需要这样一个特定的正则表达式。 Try
尝试
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);
prints 版画
["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]
You almost got it. 你几乎得到了它。 Just wrap your second term in an additional braces to capture all numbers
只需将第二个术语包装在另外的大括号中即可捕获所有数字
([a-zA-Z])((?:-?(?:\d+(?:\.\d+)?),?)+)
I changed the additional (...)
to (?:...)
, to avoid unnecessary capturing of subexpressions. 我将附加
(...)
更改为(?:...)
,以避免不必要的子表达式捕获。
Test case for capturing groups: 捕获组的测试用例:
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);
}
}
and the output 和输出
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],
This is with java -version
这是用
java -version
java version "1.7.0_03"
java版“1.7.0_03”
OpenJDK Runtime Environment (IcedTea7 2.1.1pre) (7~u3-2.1.1~pre1-1ubuntu2)OpenJDK运行时环境(IcedTea7 2.1.1pre)(7~u3-2.1.1~pre1-1ubuntu2)
OpenJDK 64-Bit Server VM (build 22.0-b10, mixed mode)OpenJDK 64位服务器VM(内置22.0-b10,混合模式)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.