Groovy web console

subscribe to the feed Subscribe
to this
site
easy regex match (via #groovywebconsole)
tweet this snippet Tweet
this
script

easy regex match

Published 9 years ago by uehaj with tags regex closure call
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
java.util.regex.Matcher.metaClass.call = { Closure c ->
  while (delegate.find()) {
    def groups = []
    (0..delegate.groupCount()).each {
      c.setProperty("\$$it", delegate.group(it))
    }
    c.call(groups)
  }
}

line = "aaa bb ccccc aa b c"

(line =~ /(a+) (b+) (c+)/) {
   println $1
   println $2
   println $3
}