IAmYourFaja
IAmYourFaja

Reputation: 56874

Double-escaping regex from inside a Groovy expression

Note: I had to simplify my actual use case to spare SO a lot of backstory. So if your first reaction to this question is: why would you ever do this, trust me, I just need to.

I'm trying to write a Groovy expression that replaces double-quotes (""") that appear in a string with single-quotes ("'").

// BEFORE: Replace my "double" quotes with 'single' quotes.
String toReplace = "Replace my \"double-quotes\" with 'single' quotes.";

// Wrong: compiler error
String replacerExpression = "toReplace.replace(""", "'");";

Binding binding = new Binding();
binding.setVariable("toReplace", toReplace);
GroovyShell shell = new GroovyShell(binding);

// AFTER: Replace my 'double' quotes with 'single' quotes.
String replacedString = (String)shell.evaluate(replacerExpression);

The problem is, I'm getting a compile error on the line where I assign replacerExpression:

Syntax error on token ""toReplace.replace("", { expected

I think it's because I need to escape the string that contains the double-quote character (""") but since it's a string-inside-a-string, I'm not sure how to properly escape it here. Any ideas?

Upvotes: 2

Views: 5041

Answers (3)

Sriharsha
Sriharsha

Reputation: 2443

Please try to use regular expressions to solve this kind of problems, instead of messing your head to tackle the escaping of quotes.

I have put up a solution using groovy console. Please see if that helps.

Upvotes: 0

ataylor
ataylor

Reputation: 66059

You need to escape the quote within quotes in this line:

String replacerExpression = "toReplace.replace(""", "'");";

The string will be evaluated twice: once as a string literal, and once as a script. This means you have to escape it with a backslash, and escape the backslash too. Also, with the embedded quotes, it'll be much more readable if you use triple quotes.

Try this (in groovy):

String replacerExpression = """toReplace.replace("\\"", "'");""";

In Java, you're stuck with using backslashes to escape all the quotes and the embedded backslash:

String replacerExpression = "toReplace.replace(\"\\\"\", \"\'\");";

Upvotes: 4

Michael Easter
Michael Easter

Reputation: 24468

Triple-quotes work well, but one can also use single-quoted string to specify a double-quote, and a double-quoted string for a single-quote.

Consider this:

String toReplace = "Replace my \"double-quotes\" with 'single' quotes." 

// key line:
String replacerExpression = """toReplace.replace('"', "'");"""

Binding binding = new Binding(); binding.setVariable("toReplace", toReplace)
GroovyShell shell = new GroovyShell(binding)

String replacedString = (String)shell.evaluate(replacerExpression)

That is, after the string literal evaluation, this is evaluated in the Groovy shell:

toReplace.replace('"', "'");

If that is too hard on the eyes, replace the "key line" above with another style (using slashy strings):

String ESC_DOUBLE_QUOTE = /'"'/ 
String ESC_SINGLE_QUOTE = /"'"/ 
String replacerExpression = """toReplace.replace(${ESC_DOUBLE_QUOTE}, ${ESC_SINGLE_QUOTE});"""

Upvotes: 1

Related Questions