Shahar Hamuzim Rajuan
Shahar Hamuzim Rajuan

Reputation: 6129

Replace . with / in groovy

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.

Upvotes: 0

Views: 3251

Answers (1)

xxxvodnikxxx
xxxvodnikxxx

Reputation: 1277

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

Upvotes: 2

Related Questions