In a previous post we learned how to create a java.util.regex.Pattern object. Now we learn how to create a java.util.regex.Matcher object and use it for finding and matching values.
In Groovy we use the =~ operator (find operator) to create a new matcher object. If the matcher has any match results we can access the results by invoking methods on the matcher object. But Groovy wouldn't by groovy if we could access the results easier. Groovy enhances the Matcher class so the data is available with an array-like syntax. If we use groups in the matcher the result can be accessed with a multidimensional array. Although the result of the =~operator is a matcher object in a conditional statement the result will be converted to a Boolean values.
We can use a second operator, ==~ (match operator), to do exact matches. With this operator the matches() method is invoked on the matcher object. The result is a Boolean value.
* [ In Action ] The Simple Groovy datatypes - Working with regular expressions
* Groovy Goodness: Using Regular Expression Pattern Class
* Groovy Goodness: Remove Part of String With Regular Expression Pattern
* Groovy Goodness: Partial Matches
* Grails Goodness: Checking Results from Forward Action in Controller Unit Tests
* Groovy Goodness: the Switch Statement