Monday, May 16, 2011

regex pattern "matches" ==~ with metaclass

Closure foundIn = { a, b ->
  print (/$a --> ${b}?? /); 
  return (a ==~ /${b}/); 
}
 
String testConditionRegex = "[abc]"

println foundIn('a', testConditionRegex) //true
println foundIn('z', testConditionRegex) //false
println foundIn(1, testConditionRegex) //false
 
println foundIn(2, 2) //true
println foundIn(12, 12.0) //false
println foundIn(null, testConditionRegex) //false
 
Object.metaClass.foundIn = { return foundIn (delegate, it); }
 
println ("4".foundIn("[1-9]")) //true
println (8.foundIn("[1-9]")) //true
println ((-8).foundIn("[1-9]")) //false

see: http://groovyconsole.appspot.com/script/485002

No comments:

Post a Comment