illegal string body character after dollar sign

前端 未结 4 1238
既然无缘
既然无缘 2020-12-25 14:33

if i define a groovy variable

def x = \"anish$\"

it will throw me error, the fix is

def x = \"anish\\$\"

相关标签:
4条回答
  • 2020-12-25 15:15

    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...")
    
    0 讨论(0)
  • 2020-12-25 15:15

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

    def x = "anish" + '$'
    
    0 讨论(0)
  • 2020-12-25 15:17

    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'

    0 讨论(0)
  • 2020-12-25 15:19

    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\\'
    
    0 讨论(0)
提交回复
热议问题