Search code examples
groovyreplaceall

replacing escaped quotes in groovy


I am unsure why this doesn't work:

  string.replaceAll('\\"','"')

I want to replace all \" with "

Any idea?

I have also tried

 string.replaceAll("[\"]","\"") 

Solution

  • The first argument to the replaceAll method is a regular expression, so the backslash character has significance there and needs to be escaped. You could use the forward-slash string delimiter to avoid double-escaping.

    assert (/Hello, \"Joe\"/.replaceAll(/\\"/, '"') == 'Hello, "Joe"')