JDK-4968604 : replace string error with special string/char
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util.regex
  • Affected Version: 1.4.2
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2003-12-15
  • Updated: 2004-04-27
  • Resolved: 2004-04-27
Related Reports
Duplicate :  
Description

Name: gm110360			Date: 12/15/2003


FULL PRODUCT VERSION :
java version "1.4.2_02"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_02-b03)
Java HotSpot(TM) Client VM (build 1.4.2_02-b03, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
I think all platform

EXTRA RELEVANT SYSTEM CONFIGURATION :
nothing

A DESCRIPTION OF THE PROBLEM :
Error when replace string "\\" by "/",  or replace string "/" by "\\". or replace other more longer string contains "/" or "\\".


STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
sample code:
    String str = "abcd\\efg//hijk:lmn";
    System.out.println("str = " + str);
    str = str.replaceAll("/", "\\");          //error,
    System.out.println("after replace / with \\ =" + str);
    str = str.replaceAll("\\", "/");         //error,
    System.out.println("after replace \\ with / =" + str);

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
expect string replace ok.
Other if need to replace by other way, the Java doc of Class String should point out this.
ACTUAL -
compile ok.
runtime error.

ERROR MESSAGES/STACK TRACES THAT OCCUR :
1. error message of code str = str.replaceAll("/", "\\");
java.lang.StringIndexOutOfBoundsException: String index out of range: 1
	at java.lang.String.charAt(String.java:460)
	at java.util.regex.Matcher.appendReplacement(Matcher.java:544)
	at java.util.regex.Matcher.replaceAll(Matcher.java:646)
	at java.lang.String.replaceAll(String.java:1710)

2. error message of code str = str.replaceAll("\\", "/");
java.util.regex.PatternSyntaxException: Unexpected internal error near index 1
 ^
	at java.util.regex.Pattern.error(Pattern.java:1489)
	at java.util.regex.Pattern.compile(Pattern.java:1281)
	at java.util.regex.Pattern.<init>(Pattern.java:1030)
	at java.util.regex.Pattern.compile(Pattern.java:777)
	at java.lang.String.replaceAll(String.java:1710)

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
    String str = "abcd\\efg//hijk:lmn";
    System.out.println("str = " + str);
    str = str.replaceAll("/", "\\");          //error,
    System.out.println("after replace / with \\ =" + str);
    str = str.replaceAll("\\", "/");         //error,
    System.out.println("after replace \\ with / =" + str);
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
maybe should modify function
public Matcher appendReplacement(StringBuffer sb, String replacement)
in java.util.regex.Matcher
(Incident Review ID: 230979) 
======================================================================

Comments
EVALUATION This is not a bug because the replacement string is not literal for these methods (there are methods added in Tiger for literal replacement). However the exceptions that are currently thrown can be confusing and should be improved. ###@###.### 2003-12-15 This is actually a duplicate of two bugs, 5024613 and 4783892. Both of those issues will be addressed by providing a more helpful message to the user when a regex or replacement has an unfinished escape sequence. ###@###.### 2004-04-27
27-04-2004