我正在尝试将正则表达式从有效匹配和println测试每个...有效的数据形式应该是schedules[##]
(#
是一个数字)
def valid = ~/^('schedules')('[')[0-9]{2}(']')$/
params.entrySet().findAll {
valid.matcher(it.key).matches()
}.each {
println("Testing")
}
我也试过~/^('schedules[')[0-9]{2}(']')$/
答案 0 :(得分:0)
试试这个:
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