JDK-4689326 : Error in String.replaceAll(String, String) for regex of "."
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 1.4.0
  • Priority: P3
  • Status: Closed
  • Resolution: Not an Issue
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2002-05-22
  • Updated: 2002-05-22
  • Resolved: 2002-05-22
Related Reports
Relates :  
Description

Name: rmT116609			Date: 05/22/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 :
Windows 2000 SP2

A DESCRIPTION OF THE PROBLEM :
The following command:

String s;
s = "test.help.kunde";
System.out.println(s.replaceAll(".","_"));

Results in:
_______________

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1.javac Test.java
2.java Test

EXPECTED VERSUS ACTUAL BEHAVIOR :
Result should be (like in replace(char, char)):

test_help_kunde

This bug can be reproduced always.

---------- BEGIN SOURCE ----------
class Test {

public static void main(String args[]) {

String s = "test.help.kunde";
System.out.println(s.replaceAll(".","_"));

}
}
---------- END SOURCE ----------

CUSTOMER WORKAROUND :
Use: replace(char, char)
(Review ID: 146882) 
======================================================================

Comments
EVALUATION The javadoc for String.replaceAll says: Replaces each substring of this string that matches the given regular expression with the given replacement. Therefore, "." is not interpretted as a single character, but as a regular expression. In Java regular expressions, '.' is defined to be any character so all characters in the provided string will match. The output is correct. One way to have the same effect as String.replace(char, char), is to escape the '.'. String.replaceAll("\\.", "_") will work. (were one '\' is to escape the '.' and the second is to escape the '\') -- iag@sfbay 2002-05-22
22-05-2002