Bug 26112 - string.replaceAll("\\")
Summary: string.replaceAll("\\")
Status: RESOLVED FIXED
Alias: None
Product: classpath
Classification: Unclassified
Component: classpath (show other bugs)
Version: 0.20
: P3 normal
Target Milestone: 0.90
Assignee: Ito Kazumitsu
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-02-05 23:35 UTC by Michael Kay
Modified: 2006-06-14 01:48 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2006-02-06 22:21:47


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Michael Kay 2006-02-05 23:35:04 UTC
Within the replacement string of replaceAll(), a double "\\" should cause a single "\" to be inserted. Thus

s = s.replaceAll("\\.", "\\\\.");

should cause any occurrence of "." to be replaced by "\.". However, it seems to cause "." to be replaced by "\\.".

Michael Kay
Comment 1 Mark Wielaard 2006-02-06 14:07:18 UTC
Confirmed. replaceAll can be use groupbackreferences through $1-$9 and this actually works. See Matcher.appendReplacement() and REMatcher.substituteInto(). But we don't properly handle escaping using backslash \.

Here is a small example:

public class ReplaceAll
{
  public static void main(String[] args) throws Exception
  {
    String s = "some.dotted.text ending in a dot.";
    s = s.replaceAll("\\.", "\\\\.");
    System.out.println(s);

    s = "Start by saying 'Hello Mark'";
    s = s.replaceAll("Hello (\\w+)",
                     "Welcome $1, do you want anything to drink, $1?");
    System.out.println(s);

    s = "All dollars are written as $";
    s = s.replaceAll("$", "\\$ (the dollar-sign)");
    System.out.println(s);
  }
}

We only handle the second replace correctly.
Comment 2 Ito Kazumitsu 2006-02-06 22:21:47 UTC
The bug is in gnu.regexp.REMatcher, and I will fix it. 
Comment 3 cvs-commit@developer.classpath.org 2006-02-09 17:49:34 UTC
Subject: Bug 26112

CVSROOT:	/cvsroot/classpath
Module name:	classpath
Branch: 	
Changes by:	Ito Kazumitsu <itokaz@savannah.gnu.org>	06/02/09 13:44:59

Modified files:
	.              : ChangeLog 
	gnu/regexp     : RE.java REMatch.java 
	java/util/regex: Matcher.java 

Log message:
	2006-02-09  Ito Kazumitsu  <kaz@maczuka.gcd.org>
	
	Fixes bug #26112
	* gnu/regexp/RE.java(REG_REPLACE_USE_BACKSLASHESCAPE): New execution
	flag which enables backslash escape in a replacement.
	(getReplacement): New public static method.
	(substituteImpl),(substituteAllImpl): Use getReplacement.
	* gnu/regexp/REMatch.java(substituteInto): Replace $n even if n>=10.
	* java/util/regex/Matcher.java(appendReplacement)
	Use RE#getReplacement.
	(replaceFirst),(replaceAll): Use RE.REG_REPLACE_USE_BACKSLASHESCAPE.

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/classpath/classpath/ChangeLog.diff?tr1=1.6313&tr2=1.6314&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/classpath/classpath/gnu/regexp/RE.java.diff?tr1=1.16&tr2=1.17&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/classpath/classpath/gnu/regexp/REMatch.java.diff?tr1=1.6&tr2=1.7&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/classpath/classpath/java/util/regex/Matcher.java.diff?tr1=1.12&tr2=1.13&r1=text&r2=text



Comment 4 Ito Kazumitsu 2006-02-09 22:15:30 UTC
Fixed. 
Comment 5 Andrew Pimlott 2006-06-14 01:48:35 UTC
I am using gcj 4.1, specifically 4.1.1-2 from Debian unstable.  Looking at the Debian source tree, it appears that only part of this fix made it into 4.1:  gnu/regexp/RE.java has the REG_REPLACE_USE_BACKSLASHESCAPE business, but java/util/regex/Matcher.java does not.  So the end behavior is still wrong.