As the docs say for replaceAll , you can use Matcher.quoteReplacement
def input = "You must pay %price%" def price = '$41.98' input.replaceAll '%price%', java.util.regex.Matcher.quoteReplacement( price )
Also note that instead of double quotes in:
replacement = "$bar"
You want to use single quotes, for example:
replacement = '$bar'
Otherwise, Groovy will treat it as a template and fail if it cannot find the bar
property.
So for your example:
import java.util.regex.Matcher assert '$bar' == 'foo'.replaceAll( 'foo', Matcher.quoteReplacement( '$bar' ) )
source share