-
Bug
-
Resolution: Fixed
-
P3
-
1.4.0
-
beta
-
generic, x86, sparc
-
generic, solaris_8, windows_2000
Name: jk109818 Date: 03/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)
A DESCRIPTION OF THE PROBLEM :
Please see:
http://java.sun.com/j2se/1.4/docs/api/java/util/regex/Matcher.html#appendReplacement(java.lang.StringBuffer, java.lang.String)
The example given for the method appendReplacement(..., ...)
in java.util.regex.Matcher is:
Pattern p = Pattern.compile("cat");
Matcher m = p.matcher("one cat two cats in the yard");
StringBuffer sb = new StringBuffer();
boolean result = m.find();
while (m.appendReplacement(sb, "dog")) {
result = m.find();
}
m.appendTail(sb);
System.out.println(sb.toString());
This is obviously wrong and won't even compile. This
should most probably be:
Pattern p = Pattern.compile("cat");
Matcher m = p.matcher("one cat two cats in the yard");
StringBuffer sb = new StringBuffer();
while (m.find()) {
m.appendReplacement(sb, "dog");
}
m.appendTail(sb);
System.out.println(sb.toString());
This bug can be reproduced always.
(Review ID: 144177)
======================================================================
- duplicates
-
JDK-4646676 Matcher.appendReplacement() example is wrong
- Closed