Java regexp groups replacements

Viewed 110

Is there a way to transform the String

"m1, m2, m3"

to the following

"m1/build, m2/build, m3/build"

only by using String.replaceAll(regex, replacement) method?

So far this is my best attempt:

System.out.println("m1, m2, m3".replaceAll("([^,]*)", "$1/build"));
>>> m1/build/build, m2/build/build, m3/build/build

As you can see the output is incorrect. However, using the same regexp in Linux's sed command gives correct output:

echo 'm1, m2, m3' | sed -e 's%\([^,]*\)%\1/build%g'
>>> m1/build, m2/build, m3/build
2 Answers
Related