[groovy-user] Problems escaping dollar sign in slashy strings

561 views
Skip to first unread message

Alexander Kleymenov

unread,
Jan 8, 2012, 9:24:41 AM1/8/12
to us...@groovy.codehaus.org
Hi,

I have a problem with following script:
---
r = /^\\\$var\$/
if ('$var' ==~ r) println "ok"
---
groovy.lang.MissingPropertyException: No such property: var for class:
test_reg_err1
at test_reg_err1.run(test_reg_err1.gr:1)

It seems escaping dollar sign does not work inside slashy strings (
/^\$var\$/ gives same result).
However it does for r = "^\\\$jit\$" .
If to use dollar slashy string it refuses to compile at all:
---
r = $/^\\\$var\$/$
if ('$var' ==~ r) println "ok"
---
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
test_reg_err1.gr: 3: unexpected char: 0xFFFF @ line 3, column 1.

(there are only 2 lines of code, and I'm using -Dfile.encoding=utf8)

Looks like a bug? Checked 1.8.2, 1.8.5

- Alexander

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


Paul King

unread,
Jan 8, 2012, 3:03:28 PM1/8/12
to us...@groovy.codehaus.org

Dollar is the escape for dollar in dollar slashy strings, i.e.
your example works like this (note there is no need to escape
backslashes in slashy strings):

r = $/^\$$var$$/$


if ('$var' ==~ r) println "ok"

For slashy strings you would do something like:
r = /^\${'$'}var${'$'}/

but that is a bit more ugly hence the reason for dollar
slashy strings.

Cheers, Paul.

Reply all
Reply to author
Forward
0 new messages