Skip to content

Instantly share code, notes, and snippets.

@melix
Created November 5, 2013 18:15
Show Gist options
  • Save melix/7323518 to your computer and use it in GitHub Desktop.
Save melix/7323518 to your computer and use it in GitHub Desktop.
Grep method for String
String.metaClass.grep = { pattern, cl ->
def matcher = delegate =~ pattern
if (matcher.find()) {
cl.doCall(*matcher.collect { it })
}
}
def address = 'Beverly Hills, 90210'.grep(/([\w\s]+),\s*(\d+)/) { match, city, zip ->
[city: city, zip: zip]
}
assert address == [city: 'Beverly Hills', zip: '90210']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment