-
Bug
-
Resolution: Fixed
-
P3
-
1.4.0, 1.4.1
-
mantis
-
x86
-
linux, windows_xp
-
Verified
Name: rmT116609 Date: 05/14/2002
FULL PRODUCT VERSION :
java version "1.4.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)
FULL OPERATING SYSTEM VERSION : Microsoft XP [Version 5.1.2600], Windows 2000
A DESCRIPTION OF THE PROBLEM :
Try to replace '.' with the path separator, on windows (e.g. convert a package name into a package path)
"my.test".replaceAll("\\.",java.io.File.separator);
results in StringIndexOutOfBoundsException.
Problem is that the regex package is parsing the replacement string - but the regex docs need to make this clearer.
The docs for java.util.regex.Matcher.replaceAll indicate:
"The replacement string may contain references to captured subsequences as in the appendReplacement method."
But the consequences of this need to be spelled out more. Particularly when the 'captured subsequence' group specifier does not use a backslash but a '$'. It
is mention that to get a '$' it must be escaped with a backslash. (I guess it doesn't use "$$" to be compatible with other regex systems).
Please document that blackslash must be escaped in the REPLACEMENT string.
NOTE: This is NOT the same as Bug #4626653 in that case the complaint was about the regular expression string parameter NOT the replacement string.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Try to be platform independent (on Windows) changing a package name into a package path:
System.out.println("hi.mom".replaceAll("\\.",java.io.File.separator);
EXPECTED VERSUS ACTUAL BEHAVIOR :
expected output is:
hi\mom
instead get StringIndexOutOfBoundsException
On Solaris 2.8 using JDK1.4.0. I got the following output.
$java test
hi/mom/
On Windows:
ERROR MESSAGES/STACK TRACES THAT OCCUR :
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 test.main(test.java:5)
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
// run on windows to see failure
// works on unix based systems
// programmer is trying to do the right thing to be platform agnostic
// but
public class test
{
static public void main(String args[])
{
// make path to package classes/source from package name
System.out.println("hi.mom.".replaceAll("\\.",java.io.File.separator));
}
}
---------- END SOURCE ----------
CUSTOMER WORKAROUND :
replacement string must have backslashed doubly escaped
(Review ID: 146538)
======================================================================
- duplicates
-
JDK-4842623 Backslash use in replacement of Matcher improperly documented
-
- Closed
-