尝试这个:
def params = [ 'schedules[32]' : 'pass 1',
'schedules[tim]' : 'fail 2',
'schedules[4]' : 'fail 3',
'schedules[09]' : 'pass 4',
'invalid' : 'fail 5' ]
params.entrySet().findAll {
// Look for start of line then 'schedules[' then 2 chars 0-9, then ] and EOL
it.key ==~ /^schedules\[([0-9]{2})]$/
}.each {
println it.value
}
因为我们已经对[0-9]{2}
位进行了分组,所以我们可以变得更复杂一些:
params.entrySet().findResults { entry ->
( entry.key =~ /^schedules\[([0-9]{2})]$/ ).with {
matches() ? [ key:entry.key, value:entry.value, num:it[0][1] ] : null
}
}.each {
println "param $it.key with number $it.num = $it.value"
}
哪个打印:
param schedules[32] with number 32 = pass 1
param schedules[09] with number 09 = pass 4