Groovy check () handle dollar sign ($)

below code cannot work

def map = [name:"Test :: ( %2f %25 \$ * & ! @ # ^)"] String s = map.inspect() println Eval.me(s) 

get error:

 Script1.groovy: 1: illegal string body character after dollar sign; solution: either escape a literal dollar sign "\$5" or bracket the value expression "${5}" @ line 1, column 30. ["name":"Test :: ( %2f %25 $ * & ! @ # ^)"] 

but if the line contains other special char like \ ", it works correctly. in any way how to walk around, this is crashing for me

+4
source share
4 answers

(In response to the foregoing)

OK, if you just want to exchange information, then you should use a data exchange format such as XML or JSON. I recommend JSON because it is lightweight, fast and very easy to use:

Computer 1

 def map = [name:"Test :: ( %2f %25 \$ * & ! @ # ^)"] def json = new groovy.json.JsonBuilder() json(map) println json.toString() 

Computer 2

 def incoming = '{"name":"Test :: ( %2f %25 $ * & ! @ # ^)"}' def jsonInput = new groovy.json.JsonSlurper() def map = jsonInput.parseText(incoming) println map 

Please note that they require Groovy 1.8.0 or later. There are many examples for older versions of Groovy, and Grails also has its own parsers.

+2
source

Use single quotes around your string:

 def map = [name:'Test :: ( %2f %25 \$ * & ! @ # ^)'] 

When you use double quotes, dollar char is used for patterns

0
source

Use single quotes and double backslashes, e.g.

 def map = [name:'Test :: ( %2f %25 \\$ * & ! @ # ^)'] String s = map.inspect() println Eval.me(s) 
0
source

I just tested the following and it worked for me

 > a = ["guy":"mogr \$ abi"] Eval.me(a.inspect())["guy"] mogr $a bi 
0
source

All Articles