History | Log In     View a printable version of the current page.  
Issue Details (XML | Word | Printable)

Key: GROOVY-2225
Type: Bug Bug
Status: Closed Closed
Resolution: Won't Fix
Priority: Major Major
Assignee: Paul King
Reporter: Eduard Penzhorn
Votes: 0
Watchers: 0
Operations

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

replaceAll cannot replace backslash

Created: 19/Oct/07 05:16 AM   Updated: 20/Oct/07 05:32 AM
Component/s: groovy-jdk
Affects Version/s: None
Fix Version/s: 1.1-rc-2

Time Tracking:
Not Specified

Environment: redhat enterprise workstation 4


 Description  « Hide
when calling the replaceAll function on java.lang.String with a backslash :

println 'xxxx'.replaceAll('x',{'\\'})

I get the following exception:

Caught: java.lang.StringIndexOutOfBoundsException: String index out of range: 1

any other character does not cause the same exception



 All   Comments   Work Log   Change History      Sort Order: Ascending order - Click to sort in descending order
Paul King - 20/Oct/07 05:32 AM
This is just how the underlying Java library works. You need 4 backslashes when using Java. Try:
println 'xyxyx'.replaceAll('x',{'\\\\'}) // =>   \y\y\

Groovy provides the slashy string syntax to save you from having to use 4 backslahes but for cases involving only backslahes, the slashy string syntax might add more confusion because of its special escaping rules for escaping slashes.