groovy

replaceAll cannot replace backslash

Details

  • Type: Bug Bug
  • Status: Closed Closed
  • Priority: Major 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 :
    0

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

Activity

Hide
Paul King added a comment -

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.

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

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: