groovy: how to replaceAll ')' with ' '

john picture john · Apr 11, 2010 · Viewed 79.9k times · Source

I tried this:

def str1="good stuff 1)"
def str2 = str1.replaceAll('\)',' ')

but i got the following error:

Exception org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed, Script11.groovy: 3: unexpected char: '\' @ line 3, column 29. 1 error at org.codehaus.groovy.control.ErrorCollector(failIfErrors:296)

so the question is how do I do this:

str1.replaceAll('\)',' ')

Answer

Tomislav Nakic-Alfirevic picture Tomislav Nakic-Alfirevic · Apr 11, 2010

Same as in Java:

def str2 = str1.replaceAll('\\)',' ')

You have to escape the backslash (with another backslash).