arhu
arhu

Reputation: 62

Extract string in jenkins pipeline(groovy)

I have a string like "AAA_revision12" and I have to extract the substring before "_" for example "AAA". I tried some regexps, but they doesn't work in jenkins.

String stringParser(String inputString) {
    inputString ? inputString.split(/_\d/)[0] : ''
}

$string = "AAA revision".split('-')

assert string[0]

Upvotes: 1

Views: 6936

Answers (1)

CommodoreBeard
CommodoreBeard

Reputation: 340

Your question is very confusing. I presume you are after a groovy snippet which will return the substring. If so:

String stringParser(String inputString) {
    inputString.split("_")[0]
}

As an example:

String input = "foo_bar"
desired = "foo"
assert desired == stringParser(input)
> True

Upvotes: 4

Related Questions