JDK-4846419 : String.replaceAll(String reg,String replacement) not working for reg = "\\"
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 1.4.2
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_2000
  • CPU: x86
  • Submitted: 2003-04-10
  • Updated: 2003-04-10
  • Resolved: 2003-04-10
Related Reports
Duplicate :  
Relates :  
Description

Name: gm110360			Date: 04/10/2003


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

FULL OS VERSION :
Linux srv1.xenia.access.com.au 2.4.18-3 #1 Thu Apr 18 07:37:53 EDT 2002 i686 unknown

A DESCRIPTION OF THE PROBLEM :
My application is running under linux box, and I was tring to replace the windows file seperator with a solaris/linux file seperator for a path String,

Code likes:
String serverPath = tempPath.replaceAll("\\",File.separator);

I got exception:

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)
        at au.com.access.deploy.agent.AgentThread.processSetFileRequest(AgentThread.java
:270)
        at au.com.access.deploy.agent.AgentThread.processMessage(AgentThread.java:134)
        at au.com.access.deploy.agent.AgentThread.run(AgentThread.java:594)

au.com.access.deploy.agent.AgentThread.processSetFileRequest : Unexpected internal error
 near index 1

 

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
String tempPath = "\\root\\temp";

String serverPath = tempPath.replaceAll("\\",File.separator);


EXPECTED VERSUS ACTUAL BEHAVIOR :
no exception caught and serverPath = "/root/temp" as expected.
I got exception:

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)
        at au.com.access.deploy.agent.AgentThread.processSetFileRequest(AgentThread.java
:270)
        at au.com.access.deploy.agent.AgentThread.processMessage(AgentThread.java:134)
        at au.com.access.deploy.agent.AgentThread.run(AgentThread.java:594)

au.com.access.deploy.agent.AgentThread.processSetFileRequest : Unexpected internal error
 near index 1



ERROR MESSAGES/STACK TRACES THAT OCCUR :
I got exception:

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)
        at au.com.access.deploy.agent.AgentThread.processSetFileRequest(AgentThread.java
:270)
        at au.com.access.deploy.agent.AgentThread.processMessage(AgentThread.java:134)
        at au.com.access.deploy.agent.AgentThread.run(AgentThread.java:594)

au.com.access.deploy.agent.AgentThread.processSetFileRequest : Unexpected internal error
 near index 1



REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.io.*;

public class test {
	public static void main(String[] args) {
		String tempPath = "\\test\\server\\test.txt";
		String serverPath = tempPath.replaceAll("\\",File.separator);
	}
}
---------- END SOURCE ----------
(Review ID: 183842) 
======================================================================

Comments
EVALUATION This is according to spec, however we will attempt to make further progress towards avoiding this kind of misunderstanding in the future. See rfe 4808962, which proposes the addition of methods to quote literal replacement strings and methods for replacement of one literal string for another literal string. ###@###.### 2003-04-10
10-04-2003