Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4652835

Javadoc incorrect for java.util.regex.Matcher.appendReplacement

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 1.4.1
    • 1.4.0
    • docs
    • 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)
      ======================================================================

            shommel Scott Hommel (Inactive)
            jkimsunw Jeffrey Kim (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: