简体   繁体   中英

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.

My IDE is phpStorm, so I guess it takes Java friendly regex patterns. I'm not Java developer though, so I do not know what extra options I have there.

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 "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, mixed mode)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM