JDK-6280695 : String.replaceFirst and other methods delete backslashes in replacement string
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.util.regex
  • Affected Version: 1.4.2,5.0,6
  • Priority: P2
  • Status: Closed
  • Resolution: Fixed
  • OS: generic,windows_2000,windows_xp
  • CPU: generic,x86
  • Submitted: 2005-06-06
  • Updated: 2010-04-02
  • Resolved: 2005-10-14
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 6
6 b57Fixed
Related Reports
Duplicate :  
Duplicate :  
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.4.2_07"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_07-b05)
Java HotSpot(TM) Client VM (build 1.4.2_07-b05, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Win XP

A DESCRIPTION OF THE PROBLEM :
import java.io.File;
public class Test
{
	public static void main(String[] argv)
	{
		String str = "file after replace ? ";
		String fname = "C:"+File.separator+"abc"+File.separator+"test";
		System.out.println(fname);
		str = str.replaceFirst("[?]", fname);
		System.out.println(str);
	}
}

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
in above program it should produce :




EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
C:\abc\test
file after replace C:\abc\test
ACTUAL -
C:\abc\test
file after replace C:abctest

has eaten up '\' from replacement string, I believe replacement string should not be parsed but just copied

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.io.File;
public class Test
{
	public static void main(String[] argv)
	{
		String str = "file after replace ? ";
		String fname = "C:"+File.separator+"abc"+File.separator+"test";
		System.out.println(fname);
		str = str.replaceFirst("[?]", fname);
		System.out.println(str);
	}
}
---------- END SOURCE ----------
###@###.### 2005-06-06 07:05:00 GMT

Comments
EVALUATION Add clear message in replaceAll and replaceFirst to explain the special treatment of backslash and dollar characters in "replacement".
06-10-2005

EVALUATION Confirmed. Here is a simpler and more portable test case: ---------------------------------------------- import java.util.regex.*; public class Bug { public static void main(String[] argv) { String str = "file after replace ? "; String fname = "C:\\abc\\test"; System.out.println(fname); System.out.println(str.replaceFirst("[?]", fname)); System.out.println(str.replaceAll("[?]", fname)); System.out.println(Pattern.compile("[?]").matcher(str).replaceFirst(fname)); } } ---------------------------------------------- This shows String.replaceAll has the same problem. The problem appears to be that String.replaceFirst, String.replaceAll, and Matcher.replaceFirst do not document special handling of backslashes in the replacement string, but they are implemented using Matcher.appendReplacement which does have documented special treatment of backslash and dollar. This is a serious violation of user expectation, but may be hard to fix. I'm moving this to util_regex, since it's clearly part of the regex "feature" ###@###.### 2005-06-06 07:46:00 GMT
06-06-2005