Reputation: 7412
if i define a groovy variable
def x = "anish$"
it will throw me error, the fix is
def x = "anish\$"
apart form "$" what are the blacklist characters that needs to be backslash,Is there a Groovy reference that lists the reserved characters. Most “language specifications” mention these details, but I don’t see it in the Groovy language spec (many “TODO” comments).
Upvotes: 31
Views: 70388
Reputation: 2549
Another alternative that is useful in Groovy templating is ${'$'}
, like:
def x = "anish${'$'}" // anish$
Interpolate the Java String '$'
into your GString.
Upvotes: 3
Reputation: 699
You can use octal representation. the character $ represents 044 in octal, then:
def x = 'anish\044'
or
def x = 'anish\044'
For example, in Java i did use like this:
def x = 'anish\044'
If you wants knows others letters or symbols converters, click here :)
Upvotes: 12
Reputation: 809
It might be a cheap method, but the following works for me.
def x = "anish" + '$'
Upvotes: 3
Reputation: 4755
The solution from tim_yates does not work in some contexts, e.g. in a Jasper report.
So if still everything with a $
sign wants to be interpreted as some variable (${varX}
), e.g. in
"xyz".replaceAll("^(.{4}).{3}.+$", "$1...")
then simply make the dollar sign a single concatenated character '$'
, e.g.
"xyz".replaceAll("^(.{4}).{3}.+"+'$', '$'+"1...")
Upvotes: 4
Reputation: 171084
Just use single quotes:
def x = 'anish$'
If this isn't possible, the only thing that's going to cause you problems is $
, as that is the templating char used by GString
(see the GString section on this page -- about half way down)
Obviously, the backslash char needs escaping as well, ie:
def x = 'anish\\'
Upvotes: 42