Meraj Rasool Meraj Rasool - 4 months ago 11
Groovy Question

Replacing a word with $ in groovy

I have the following piece of groovy code from a gradle build file I am using for replacing a word in a php file:

def str1="My application version is $app_version"
def str2 = str1.replaceAll('$app_version','2016072601')
println str2‚Äč


I want to replace $app_version with defined number in this method but somehow it is not working. Do I need to escape or do any special action to perform this replacement?

Answer

Strings with double quotes and $ are GStrings, which triggers Groovy's string interpolation. Also, replaceAll receives a regex as first argument, and $ is a special character in regex, thus, you need to double escape that too.

You can use single quotes OR you can escape the $ character in your str1:

def str1='My application version is $app_version'
def str2 = str1.replaceAll('\\$app_version','2016072601')
assert str2 == 'My application version is 2016072601'

Update: Expanding string interpolation a bit, it replaces the $ placeholder in your string with the variable value of the same name (though not immediately, as it creates an instance of GString first). It is roughly akin to this in Java:

String str1 = "My application version is " + app_version;

So, instead of replacing the variable with replaceAll, you could have a variable in your script with the name app_version, like def app_version = "2016072601" (which is kinda like @BZ.'s answer)


Update 2: By using string interpolation (as per @BZ.'s answer) or string concatenation, you don't need to use replaceAll, but you need a variable called app_version:

def app_version = '2016072601'
def str1 = "My application version is $app_version"
assert str1 == 'My application version is 2016072601'

And with string concatenation you also need a variable called app_version:

def app_version = '2016072601'
def str1 = "My application version is " + app_version
assert str1 == 'My application version is 2016072601'