Reputation: 6129
I need to replace all content after a specific character in groovy with the value of a parameter,
my string is :
env.APP_VERSION="1.9"
And I would like to replace everything after the = sign with the value of a certain parameter let's call it $PARAM.
I was able to trim everything after the = sign,
but not replace it...
result = result.substring(0, result.indexOf('APP_VERSION='));
any help would be appreciated.
Upvotes: 0
Views: 2130
Reputation: 30971
One of possible solutions is, indeed, to use regex. It should include:
(?<==)
- A positive lookbehind for =
..*
- Match all chars (up to the end).So the script can look like below:
src = 'env.APP_VERSION="1.9"'
PARAM = '"xyz"'
res = src.replaceFirst(/(?<==).*/, PARAM)
Another solution is to split the string on =
and "mount" the result string
from:
=
char.This time the processing part of the script should be:
spl = src.split('=')
res = spl[0] + '=' + PARAM
Upvotes: 1
Reputation: 20699
Without knowing about your original intentions you have 2 options:
1) Do not reinvent the wheel and use GString magic:
String ver = '1.9'
String result = "env.APP_VERSION=\"$ver\""
2) use some regex:
result = result.replaceFirst( /APP_VERSION="[^"]+"/, 'APP_VERSION="something"' )
Upvotes: 0