Skip to: Site menu | Main content

Groovy 

      Download | Documentation | Developers | Community

An agile dynamic language for the Java Platform

Regular Expressions Add comment to Wiki View in Wiki Edit Wiki page Printable Version

Groovy supports regular expressions natively using the ~"pattern" expression, which compiles a Java Pattern object from the given pattern string. Groovy also supports the =~ (create Matcher) and ==~ (matches regex) operators.

For matchers having groups, matcher[index] is either a matched String or a List of matched group Strings, since jsr-03 release.

import java.util.regex.Matcher
import java.util.regex.Pattern

assert "cheesecheese" =~ "cheese"
assert "cheesecheese" =~ /cheese/
assert "cheese" == /cheese/   /*they are both string syntaxes*/

// lets create a regex Pattern
def pattern = ~/foo/
assert pattern instanceof Pattern
assert pattern.matcher("foo").matches()

// lets create a Matcher
def matcher = "cheesecheese" =~ /cheese/
assert matcher instanceof Matcher
answer = matcher.replaceAll("edam")

// lets do some replacement
def cheese = ("cheesecheese" =~ /cheese/).replaceFirst("nice")
assert cheese == "nicecheese"

// simple group demo
// You can also match a pattern that includes groups.  First create a matcher object, either
// using the Java API, or more simply with the =~ operator.  Then, you can index the matcher
// object to find the matches.  matcher[0][1] means the 0th match of the whole pattern (with the
// =~ operator the pattern may match the string in more than one place), and the 1st group within
// that match.  Here's how it works:
def m = "foobarfoo" =~ /o(b.*r)f/
assert m[0][1] == "bar"

// fancier group demo
matcher = "\$abc." =~ "\\\$(.*)\\."
matcher.matches(); // must be invoked   [Question: is this still true?  Not in my experience with jsr-04.]
assert matcher.group(1) == "abc"  // is one, not zero
// assert matcher[1] == "abc"            // This has worked before jsr-03-release
assert matcher[0] == ["\$abc.", "abc"]   // But this should work since jsr-03-release
assert matcher[0][1] == "abc"            // This should work since jsr-03-release

The pattern can be expressed more simply using the "/" delimiter for the pattern, so we don't have to double all the backslashes.

def matcher = "\$abc." =~ /\$(.*)\./ // no need to double-escape!
assert "\\\$(.*)\\." == /\$(.*)\./
matcher.matches(); // must be invoked
assert matcher.group(1) == "abc"  // is one, not zero
// assert matcher[1] == "abc"            // This has worked before jsr-03-release
assert matcher[0] == ["\$abc.", "abc"]   // But this should work since jsr-03-release
assert matcher[0][1] == "abc"            // This should work since jsr-03-release

Since a Matcher coerces to a boolean by calling its find method, the =~ operator is consistent with the simple use of Perl's =~ operator, when it appears as a predicate (in 'if', 'while', etc.). The "stricter-looking" ==~ operator requires an exact match of the whole subject string.

Regular expression support is imported from Java. Java's regular expression language and API is documented here.

More Examples

def before='''
apple
orange
banana
'''

def expected='''
Apple
Orange
Banana
'''

assert expected == before.replaceAll(/(?m)^\w+/,{it[0].toUpperCase() + it[1..-1]})