I am not very good with string variables, especially in jenkins groovy, hence my question.
How can we in jenkins groovy extract a part of a string from some string variable using a found pattern?
For example, we have a string:
"The sun slowly descended below the horizon, casting vibrant hues of orange and {{ pink }} across the tranquil sea."
And we are guaranteed to know that there are {{ * }} in that line.
What is the simplest and easiest way to equate the * that is between the {{ }} to some variable?
I honestly don't even know how to approach this.
Is it possible to do something like this:
def line = "The sun slowly descended below the horizon, casting vibrant hues of orange and {{ pink }} across the tranquil sea."
def s = line.regex(.*{{.*}}.*)
def findColor = s[1]
?
Obviously it doesn't work, but I think it roughly outlines the end goal
This one will probably work:
def line = "The sun slowly descended below the horizon, casting vibrant hues of orange and {{ pink }} across the tranquil sea."
a = line.split("{{")
b = a[1]
c = b.split("}}")
d = c[0]
But it's a terribly ugly solution and I'd like to know if it could be done concisely, beautifully, reliably and understandably all in one.
You could try a regular expression:
def line = "The sun slowly descended below the horizon, casting vibrant hues of orange and {{ pink }} across the tranquil sea."
def color = (line =~ /\{\{ (.+) }}/)[0][1]
assert color == 'pink'