WhoAmI
WhoAmI

Reputation: 1133

Filter the output in the brackets as a list

I have a lines like :

   [Something-26543] One ticket
   [Something-23121] Second ticket
   [Something-21243] Third ticket and so on

Can someone advice if there is a a way to grep only the line between the square brackets. Before:

[Something-26543] Another Ticket

After filtering:

Something-26543
Something-23121
Something-21243

What I wrote as an example. Note "asdfasdf" is not between any brackets, so it should not be matched:

 def changeString = ['[DPDHLPA-26607] Updating robots.txt', '[DPDHLPA-2321] [DPDHLPA-2322] Updating something.txt', 'asdfasdf']


 def TICKET_LIST = (changeString =~ /(?<=\[)[^]]+(?=\]/[0].readLines().unique().sort().join('\n')
        print TICKET_LIST //to see if it got the filtered list

But then TICKET LIST shows only the first filter. I need to collect all outputs, because later I will do for loop on "TICKET_LIST" and assemble an URL. I've tried also with collectMany, but that didn't really work for me.

Upvotes: 0

Views: 166

Answers (1)

tim_yates
tim_yates

Reputation: 171084

You should be able to do:

def ticketList = changeString.findResults { str -> 
    (str =~ /\[([^]]+)].*/).with { 
        it.matches() ? it[0][1] : null
    }
}

Upvotes: 2

Related Questions