Details
-
Type:
Bug
-
Status:
Closed
-
Priority:
Major
-
Resolution: Won't Fix
-
Affects Version/s: None
-
Fix Version/s: 1.1-rc-2
-
Component/s: groovy-jdk
-
Labels:None
-
Environment:redhat enterprise workstation 4
-
Number of attachments :
Description
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
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.
println 'xyxyx'.replaceAll('x',{'\\\\'}) // => \y\y\