将括号中的输出过滤为列表

时间:2021-03-29 18:03:44

标签: groovy devops jenkins-groovy

我有这样一行:

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

有人可以建议是否有办法只对方括号之间的行进行 grep。 之前:

[Something-26543] Another Ticket

过滤后:

Something-26543
Something-23121
Something-21243

我以我写的为例。注意“asdfasdf”不在任何括号之间,因此不应匹配:

 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

但是 TICKET LIST 只显示第一个过滤器。我需要收集所有输出,因为稍后我将在“TICKET_LIST”上执行 for 循环并组合一个 URL。我也尝试过使用 collectMany,但这对我来说并不奏效。

1 个答案:

答案 0 :(得分:2)

你应该能够做到:

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