Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

illegal string body character after dollar sign

Tags:

groovy

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).

like image 535
anish Avatar asked Nov 01 '11 14:11

anish


5 Answers

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\\'
like image 161
tim_yates Avatar answered Oct 20 '22 05:10

tim_yates


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 :)

like image 41
4 revs Avatar answered Oct 20 '22 06:10

4 revs


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...")
like image 4
Andreas Covidiot Avatar answered Oct 20 '22 05:10

Andreas Covidiot


It might be a cheap method, but the following works for me.

def x = "anish" + '$'
like image 3
Letokteren Avatar answered Oct 20 '22 04:10

Letokteren


Another alternative that is useful in Groovy templating is ${'$'}, like:

def x = "anish${'$'}" // anish$

Interpolate the Java String '$' into your GString.

like image 1
whitehat101 Avatar answered Oct 20 '22 05:10

whitehat101