Reputation: 1133
def aString = '[Something-26607] Updating another.txt [Something2] Updating something.txt [Something2] Updating something.txt fsfsfs'
Is there a way in groovy to get only unique parts between the square brackets and store them in a variable so after I can call them in a for loop. So I will iterate over them
Something-26607
Something2
I have tried:
def matcher = aString =~ (/\[(?<issue>[^]]+)]/)
def result = matcher.find() ? matcher.group('issue') : ""
print result
but I get only get the first match:
Something-26607
Upvotes: 0
Views: 2207
Reputation: 163217
I have no experience in Groovy, but reading a little bit about the syntax to get an example, you might use a Set and loop the results using a while loop:
def myList = []
def aString = '[Something-26607] Updating another.txt [Something2] Updating something.txt [Something2] Updating something.txt fsfsfs'
def pattern = Pattern.compile("\\[(?<issue>[^]]+)]")
def matcher = pattern.matcher(aString)
while (matcher.find()) {
myList << matcher.group(1)
}
println myList.toSet()
Output
[Something2, Something-26607]
Without using a Set, you can also use a negative lookahead to check to get the unique values in the string asserting that what is captured in the group does not occur anymore at the right side.
\\[(?<issue>[^]]+)](?!.*\\1)
Upvotes: 1