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
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.
The bug is in gnu.regexp.REMatcher, and I will fix it.
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
Fixed.
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.