Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

In Groovy, subtract a regex Pattern from a string to remove the first matching substring: text - ~/pattern/. For example, 'Line contains 20 characters' - ~/d+s+/ returns 'Line contains characters'. Use replaceAll instead when you want to remove every match.

Remove the first regex match with subtraction

Groovy’s tilde-slashy notation creates a regular-expression Pattern. The string subtraction operator accepts that pattern, removes the first substring it matches, and returns the remaining text.

def text = 'Line contains 20 characters'
def result = text - ~/d+s+/

assert result == 'Line contains characters'

The pattern d+s+ matches the digits and the whitespace immediately after them, so both are removed. Pattern subtraction is described as available since Groovy 2.2; check the version used by your project if it targets an older runtime. The Groovy Goodness Notebook sample gives the feature’s version note and examples.

Make the pattern match exactly what should disappear

Subtraction removes the exact span matched by the pattern. Include neighboring spaces or punctuation in the pattern if those should be deleted too; otherwise, the surrounding text remains unchanged.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
def wordStartsWithGr = ~/(?i)s+Grw+/

assert ('Hello Groovy world!' - wordStartsWithGr) == 'Hello world!'
assert ('Hi Grails users' - wordStartsWithGr) == 'Hi users'

The inline (?i) makes the match case-insensitive, and s+ includes the whitespace before the word. A word-boundary pattern illustrates what happens when spaces are not part of the match:

assert ('Remove first match of 5 letter word' - ~/bw{5}b/) == 'Remove  match of 5 letter word'

The matched word is deleted, but its adjacent spaces remain, producing two spaces in the result. Adjust the pattern to include the spacing you want removed.

Choose subtraction, replaceFirst, or replaceAll

Intent Groovy expression Effect
Delete the first matching substring text - ~/pattern/ Removes the first match.
Delete the first match with an explicit replacement text.replaceFirst(/pattern/, '') Replaces the first match with an empty string.
Delete every matching substring text.replaceAll(/pattern/, '') Replaces all matches with an empty string.

Use subtraction when its concise first-match behavior is what you want. The Groovy API documents replaceFirst as replacing the first matching substring and replaceAll as replacing all matching substrings. The Groovy 2.3.2 StringGroovyMethods API describes these methods and their replacement-string handling; the Groovy 2.3.5 String enhancements API lists the related string methods.

When replacement text contains dollar signs or backslashes

Replacement strings passed to replaceFirst and replaceAll interpret dollar signs and backslashes specially. If replacement text must be inserted literally, wrap it with Matcher.quoteReplacement:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
import java.util.regex.Matcher

def literal = '$5\folder'
def result = text.replaceFirst(/pattern/, Matcher.quoteReplacement(literal))

This applies to replacement strings, not to the regex pattern itself. Pattern subtraction removes a match rather than inserting replacement text.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Check compatibility with the project’s Groovy version

The Groovy Goodness Notebook says regex Pattern subtraction has been available since Groovy 2.2 and labels its examples as written with Groovy 2.2. Its Strings sample provides that version context. These references do not establish a complete compatibility matrix for every later runtime, so verify the behavior against the Groovy version your application actually uses.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.