Question: I am trying to get some data from Annotations:

```
Annotations:            deployment.kubernetes.io/revision: 1
                        kubernetes.io/change-cause:
                          Branch=master-9eb8251 Message=Revert "ON-1796 Re-write the cart GET for edit cart (#1256)" (#1284)

                          This reverts commit 27e600f83ecd98923713e2996d0aeea0da2df8fa. Author=Gautam Prajapati Timestamp=2019-07-29T16:26:32.622777

```

I want to get the result as (only branch name):

`master` 

I am using the following command:

```
System.out.println(/*"branch:" +*/ result.getMetadata().getAnnotations().get("kubernetes.io/change-cause").replaceAll("Message=.*[^/]*$", "").replaceAll("Branch=", "").replaceAll("-[^-]+$", ""));

```

I get the errors because this is compatible only with Java not Groovy.

Answer: 
You may extract the value after `Branch=` using:

```
String s = 'Annotations: deployment.kubernetes.io/revision: 1 kubernetes.io/change-cause: Branch=master-9eb8251 Message=Revert "ON-1796 Re-write the cart GET for edit cart (#1256)" (#1284) This reverts commit 27e600f83ecd98923713e2996d0aeea0da2df8fa. Author=Gautam Prajapati Timestamp=2019-07-29T16:26:32.622777'
def m = (s =~ /Branch=(\S*?)(?:-[^\s-]*)?(?!\S)/ )
if (m) {
    print(m.group(1))
}

```

See the [Groovy demo](https://ideone.com/co2v7G), output - `master`. 

Also, see the [regex demo](https://regex101.com/r/0TByE2/4). **Details**:

* `Branch=` - literal string
* `(\S*?)` - Group 1 (the required value): any 0 or more non-whitespace chars as few as possible, but followed with
* `(?:-[^\s-]*)?` - an optional sequence of `-` and then 0+ chars other than whitespace and hyphen
* `(?!\S)` - followed with a whitespace or end of string.
you can use this to remove version tag from your branch :

```
import java.util.regex.Pattern
import java.util.regex.Matcher
paragraph = 'Annotations:            deployment.kubernetes.io/revision: 1                        kubernetes.io/change-cause:                          Branch=master-9eb8251 Message=Revert "ON-1796 Re-write the cart GET for edit cart (#1256)" (#1284)                          This reverts commit 27e600f83ecd98923713e2996d0aeea0da2df8fa. Author=Gautam Prajapati Timestamp=2019-07-29T16:26:32.622777'

Pattern regex = ~ /Branch=\w*/
matcher = paragraph =~ regex

println matcher[0]

```

it will stop at '-', but your branch name can have digits or '\_'. depending on your needs you can use \s or other options

result is Branch=master

If your branch name contains '-', you'll have to modify some things : 
first modify the regex 

```
Pattern regex = ~ /Branch=(\w*-\w*)*/

```

in the example change the branch name to Branch=master-TEST-test-9eb8251
then add some post processing on the resulting string :

```
myBranch = matcher[0][0] - matcher[0][0].split('-')[-1] // to remove the part after the last '-'
myBranch = myBranch.substring(0,myBranch.length() -1) // to remove the hyphen itself
println myBranch

```

you'll end up with Branch=master-TEST-test