JDK-4689750 : String.ReplaceAll() gives java.lang.StringIndexOutOfBoundsException
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util.regex
  • Affected Version: 1.4.1,1.4.2
  • Priority: P2
  • Status: Closed
  • Resolution: Not an Issue
  • OS: solaris_8,windows_2000
  • CPU: x86,sparc
  • Submitted: 2002-05-22
  • Updated: 2002-05-22
  • Resolved: 2002-05-22
Related Reports
Duplicate :  
Description
Following is the test program 
----------------------------
public class slash {
        public static void main (String str[]){
                String s = "HixyzHelloxyz";
                System.out.println(s);
                //s = s.replaceAll("xyz","$");
                s = s.replaceAll("xyz","\\");
                System.out.println(s);
        }
}

$ /net/sqindia/export/disk01/jdk/1.4.1/beta/b12/binaries/solsparc/bin/java -version
java version "1.4.1-beta"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1-beta-b12)
Java HotSpot(TM) Client VM (build 1.4.1-beta-b12, mixed mode)

Output:
---------
HixyzHelloxyz
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
        at java.lang.String.charAt(String.java:455)
        at java.util.regex.Matcher.appendReplacement(Matcher.java:532)
        at java.util.regex.Matcher.replaceAll(Matcher.java:636)
        at java.lang.String.replaceAll(String.java:1706)
        at slash.main(slash.java:6)

Spec says 'An invocation of this (replaceAll) method of the form str. replaceAll (regex, repl) yields exactly the same result as the expression
Pattern.compile(regex).matcher(str).replaceAll(repl)

However, this is also results into same error.
For Example: 
import java.util.regex.*;
public class t{
        public static void main (String str[]){
                String s = "HixyzHelloxyz";
                Pattern.compile("xyz").matcher(s).replaceAll("$");
                System.out.println(s);
        }
}

This also exists in Merlin- FCS (b92).

Comments
EVALUATION Spec also says "Note that backslashes and dollar signs in the replacement string may cause the results to be different than if it were being treated as a literal replacement string. Dollar signs may be treated as references to captured subsequences as described above, and backslashes are used to escape literal characters in the replacement string." ###@###.### 2002-05-22
22-05-2002