Skip to: Site menu | Main content

Groovy 

      Download | Documentation | Developers | Community

An agile dynamic language for the Java Platform

FAQ - RegExp Add comment to Wiki View in Wiki Edit Wiki page Printable Version

RegExp

matcher.maches() returns false

Why this code fails ?

def matcher = "/home/me/script/test.groovy" =~ /\.groovy/
assert  matcher.matches()

Because of you think you do something like "Oh dear it contains the word!", but you're confusing matches with find

From Javadoc: http://java.sun.com/j2se/1.5.0/docs/api/java/util/regex/Matcher.html#matches()

public boolean matches()

Attempts to match the entire region against the pattern.

...

So "/\.groovy/" is just a subsequence.

You must use

def matcher =  "/home/me/script/test.groovy" =~ /.*\.groovy/

What is the difference between =~ and ==~ ?

Pattern, Matcher ?

A pattern is not very usefull alone. He's just waiting input to process through a matcher.

def pattern =  ~/groovy/
def matcher = pattern.matcher('my groovy buddy')

Matcher can say a lot of thing to you:

  • if the entire input sequence matches the pattern, with matcher.matches() ;
  • if just a subsequence of the input sequence matches the pattern, with matcher.find().

A matcher with /groovy/ pattern finds a matching subsequence in the 'my groovy buddy' sequence.

On the contrary the whole sequence doesn't match the pattern.

def m = c.matcher('my groovy buddy')
assert m.find()
assert m.matches() == false

Application: to filter a list of names.

def list = ['/a/b/c.groovy', 'myscript.groovy', 'groovy.rc', 'potatoes']
// find all items whom a subsequence matches /groovy/
println list.findAll{ it =~ /groovy/ }  // => ["groovy", "/a/b/c.groovy", "myscript.groovy", "groovy.rc", "groovy."]

// find all items who match exactly /groovy/
println list.findAll{ it ==~ /groovy/ } // => ["groovy"]

// find all items who match fully /groovy\..*/ ('groovy' with a dot and zero or more char trailing)
println list.findAll{ it ==~ /groovy\..*/ } // => ["groovy.rc", "groovy."]

A little tilde headache ? Remember like this

~ the pattern
=~ roughly as the pattern (easy to write)
==~ more than roughly, exactly as the pattern (think hard...)