Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Groovy script to split label values

Omprakash Thamsetty
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 9, 2021

Need help on splitting the label values based on text.

Label field values are [June_2019_POA1, ABC, July_2020_POA2, XYZ]

So I need to find POA1 and POA2 values exist or not in label.

If the values exist then get them and assign the values to 2 different variables.

FirstPOA1 = JUNE_2019

SecondPOA2 = July_2020

Can anyone help on this plz?

1 answer

1 accepted

0 votes
Answer accepted
PD Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 9, 2021

Regex will be your friend for that.

I often use this for reference: https://regex101.com/r/1BscV3/2

Here is how you can use that in groovy:

def labels = ['June_2019_POA1', 'ABC', 'July_2020_POA2', 'XYZ']
def regexPat = /^([a-z|A-Z]*_\d{4})_(\S*)$/
def fieldMap = [POA1:'FirstPOA1', POA2:'SecondPOA2']
def FirstPOA1, SecondPOA2
labels.each{ label->
def matcher = label =~ regexPat
if(matcher.matches()){
if(matcher[0][2] == 'POA1') FirstPOA1 = matcher[0][1]
if(matcher[0][2] == 'POA2') SecondPOA2 = matcher[0][1]

}
}
log.info "FirstPOA1 = $FirstPOA1"
log.info "SecondPOA2 = $SecondPOA2"
Omprakash Thamsetty
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 11, 2021

Hi @PD Sheehan This script works but need one small changes on it to convert the string to date format. I am looking the output format would be like 06/01/2019 and 07/01/2020.

 

def fmtfrsPOA1 = FirstPOA1.replaceAll("_",",")
Date dtfirstPOA1 = (Date)fmtfrsPOA1
def dtfirstPOA1format = dtfirstPOA1.format('MM-yyyy')

I tried above but through error. Any help much appreciated. 

PD Sheehan
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 11, 2021

Try parsing the poa value as-is by providing the existing format (rather than converting), then format it in the desired format.

new Date().parse( 'MMMM_yyyy',FirstPOA1 as String).format('MM/dd/yyyy')
Omprakash Thamsetty
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 11, 2021

Its worked. You have solved my issue. Thank you so much

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
8.13.4
TAGS
AUG Leaders

Atlassian Community Events