groovy

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

Details

  • Type: Bug Bug
  • Status: Closed Closed
  • Priority: Minor Minor
  • Resolution: Fixed
  • Affects Version/s: 1.5.6
  • Fix Version/s: 1.8-beta-1
  • Component/s: regular expressions
  • Labels:
    None
  • Number of attachments :
    0

Description

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)

Activity

Hide
Peter Ledbrook added a comment -

Note that:

println 'Hi, $aaa'.replaceAll("\\\$aaa", 'Sergey')

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

Show
Peter Ledbrook added a comment - Note that:
println 'Hi, $aaa'.replaceAll("\\\$aaa", 'Sergey')
works, so it's something specific to the slash form of strings.
Hide
blackdrag blackdrag added a comment -

Paul, what should we do about this?

Show
blackdrag blackdrag added a comment - Paul, what should we do about this?
Hide
Paul King added a comment -

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.

Show
Paul King added a comment - 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.
Hide
Paul King added a comment -

Sergey, are the suggested workarounds good enough for you for 1.5.7? If so, I will change the fix version to 1.6.

Show
Paul King added a comment - Sergey, are the suggested workarounds good enough for you for 1.5.7? If so, I will change the fix version to 1.6.
Hide
Sergey Nebolsin added a comment -

Sure, Paul

Show
Sergey Nebolsin added a comment - Sure, Paul
Hide
Paul King added a comment -

I think we can close this as fixed in 1.8 as the dollar slashy string syntax covers this case.
So the other solution to this problem would be:

println 'Hi, $aaa'.replaceAll($/\$$aaa/$, 'Sergey')
Show
Paul King added a comment - I think we can close this as fixed in 1.8 as the dollar slashy string syntax covers this case. So the other solution to this problem would be:
println 'Hi, $aaa'.replaceAll($/\$$aaa/$, 'Sergey')

People

Vote (0)
Watch (1)

Dates

  • Created:
    Updated:
    Resolved: