Issue Details (XML | Word | Printable)

Key: GROOVY-2991
Type: Bug Bug
Status: Open Open
Priority: Minor Minor
Assignee: Paul King
Reporter: Sergey Nebolsin
Votes: 0
Watchers: 1
Operations

If you were logged in you would be able to see more operations.
groovy

It looks like there's no way to escape $ character in the regexp string

Created: 08/Aug/08 03:44 AM   Updated: 22/Oct/08 10:19 AM
Component/s: regular expressions
Affects Version/s: 1.5.6
Fix Version/s: 1.7-beta-1

Time Tracking:
Not Specified


 Description  « Hide
Logically, $ character in the regexp string should be escaped by two backslashes, one for preventing GString property lookup, and another to escape regexp's special meaning of $. But it doesn't seem to work with Groovy 1.5.6:

groovy> println 'Hi, $aaa'.replaceAll(/
$aaa/, 'Sergey')

Exception thrown: groovy.lang.MissingPropertyException: No such property: aaa for class: Script5
groovy.lang.MissingPropertyException: No such property: aaa for class: Script5
at Script5.run(Script5:1)



 All   Comments   Work Log   Change History      Sort Order: Ascending order - Click to sort in descending order
Peter Ledbrook added a comment - 08/Aug/08 03:54 AM
Note that:
println 'Hi, $aaa'.replaceAll("\\\$aaa", 'Sergey')

works, so it's something specific to the slash form of strings.


Jochen Theodorou added a comment - 08/Aug/08 04:47 AM
Paul, what should we do about this?

Paul King added a comment - 08/Aug/08 09:51 PM
Yes, at the moment you have to do either one of these:
println 'Hi, $aaa'.replaceAll(/\${'$'}aaa/, 'Sergey')
println 'Hi, $aaa'.replaceAll(/\$${''}aaa/, 'Sergey')

I think the solution is to change how we do escaping for slashy strings.
This is a breaking change but an incremental path has been suggested:
http://docs.codehaus.org/display/GroovyJSR/Groovy+String+Handling
under 'Slashy String improvement Proposal'. I guess it needs further discussion.


Paul King added a comment - 28/Aug/08 08:51 PM
Sergey, are the suggested workarounds good enough for you for 1.5.7? If so, I will change the fix version to 1.6.

Sergey Nebolsin added a comment - 29/Aug/08 02:05 AM
Sure, Paul