Firstly, [one:1, two:2]this is a map:
assert [one:1, two:2] instanceof java.util.Map
assert 1 == [one:1, two:2]['one']
assert 2 == [one:1, two:2]['two']
assert 1 == [one:1, two:2].get('one')
assert 2 == [one:1, two:2].get('two')
, , one 1 two 2.
-, , :
, , groupCount -. groupCount int , . groupCount 4, , 4 .
, 0, . , groupCount. , (? , .
:
def m = 'one fish, two fish' =~ /([a-z]{3})\s([a-z]{4})/
assert m instanceof java.util.regex.Matcher
m.each { group ->
println group
}
:
[one fish, one, fish] // only this first match for "replaceFirst"
[two fish, two, fish]
, it group (it- ):
assert '1-FISH, two fish' == "one fish, two fish".replaceFirst(/([a-z]{3})\s([a-z]{4})/) { group ->
[one:1, two:2][group[1]] + '-' + group[2].toUpperCase()
}
assert '1-FISH, 2-FISH' == "one fish, two fish".replaceAll(/([a-z]{3})\s([a-z]{4})/) { group ->
[one:1, two:2][group[1]] + '-' + group[2].toUpperCase()
}