0

I know this is a very specific question by I didn't manage to do the replacement myself, I need to replace this string in groovy:

com.pantest in com/pantest.

I tried this:

groupId =com.pantest
def mavenGroupID = groupId.replaceAll('.','/')

And this is what I get in the output:

echo mavenGroupID is //////////
mavenGroupID is //////////

Is the dot (.) is some kind of special character? I tried to escape it using **** but it also didn't work.

Shahar Hamuzim Rajuan
  • 5,610
  • 9
  • 53
  • 91
  • `public String replaceAll(String regex, String replacement) { ` , try `\.` – xxxvodnikxxx May 16 '19 at 12:43
  • based on https://stackoverflow.com/questions/13989640/regular-expression-to-match-a-dot should works as suggested escaping with `\` so `\.` or put it to match group via `[.]` (replaceAll is taking regex as input) – xxxvodnikxxx May 16 '19 at 13:03

1 Answers1

2

As mentioned in the comments, String.replaceAll is taking regex as the input, so it means you need to escape dot at least, but actually, you have also to escape escaping char- backslash \ (more clues at Regular Expression to match a dot)

so, you can do it like follows:

def test = "aaa.bbb.ccc"
//want to replace ., need to use escape char \, but it needs to be escaped as well , so \\
println test.replaceAll('\\.','/')

Output is as requested aaa/bbb/ccc

replaceAll('\\.','/') is the key

xxxvodnikxxx
  • 1,270
  • 2
  • 18
  • 37