Regular Expressions in Groovy

Our example will demonstrate common regular expression tasks in Groovy. Here’s the full source code:

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

// This tests whether a pattern matches a string.
def match = "peach" =~ /p([a-z]+)ch/
println match.matches()

// For other regex tasks, you'll need to create a Pattern object.
def pattern = ~/p([a-z]+)ch/

// Many methods are available on these objects. Here's a match test like we saw earlier.
println pattern.matcher("peach").matches()

// This finds the match for the regex.
def matcher = "peach punch" =~ pattern
println matcher[0]

// This also finds the first match but returns the start and end indexes for the match.
matcher = "peach punch" =~ pattern
println "idx: ${matcher.start()}, ${matcher.end()}"

// The find() method includes information about both the whole-pattern matches 
// and the submatches within those matches.
matcher = "peach punch" =~ pattern
if (matcher.find()) {
    println [matcher.group(0), matcher.group(1)]
}

// Similarly, this will return information about the indexes of matches and submatches.
matcher = "peach punch" =~ pattern
if (matcher.find()) {
    println [matcher.start(0), matcher.end(0), matcher.start(1), matcher.end(1)]
}

// To find all matches for a regex:
println ("peach punch pinch" =~ pattern).findAll()

// These 'all' variants are available for other operations as well.
matcher = "peach punch pinch" =~ pattern
def allMatches = []
while (matcher.find()) {
    allMatches << [matcher.start(0), matcher.end(0), matcher.start(1), matcher.end(1)]
}
println "all: $allMatches"

// Providing a limit to findAll will restrict the number of matches.
println ("peach punch pinch" =~ pattern).findAll(2)

// The =~ operator creates a Matcher object, which can be used directly in boolean contexts.
println ("peach" =~ pattern).asBoolean()

// When creating global variables with regular expressions, you can use the ~ operator.
pattern = ~/p([a-z]+)ch/
println "pattern: $pattern"

// The replaceAll method can be used to replace subsets of strings with other values.
println "a peach".replaceAll(pattern, "<fruit>")

// The replaceAll method also accepts a closure for more complex replacements.
def result = "a peach".replaceAll(pattern) { match, group ->
    return match.toUpperCase()
}
println result

To run the program, save it as RegularExpressions.groovy and use the groovy command:

$ groovy RegularExpressions.groovy
true
true
peach
idx: 0, 5
[peach, ea]
[0, 5, 1, 3]
[peach, punch, pinch]
all: [[0, 5, 1, 3], [6, 11, 7, 9], [12, 17, 13, 15]]
[peach, punch]
true
pattern: p([a-z]+)ch
a <fruit>
a PEACH

Groovy provides powerful built-in support for regular expressions. The =~ operator creates a Matcher object, while the ==~ operator performs a strict match. The ~ operator creates a Pattern object.

For a complete reference on Groovy regular expressions, check the official Groovy documentation.