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

Matcher.appendReplacement can't handle two adjecent group refs

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 1.4.0
    • 1.4.0
    • core-libs
    • beta3
    • generic
    • generic
    • Verified



      Name: nt126004 Date: 08/29/2001


      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta2-b77)
      Java HotSpot(TM) Client VM (build 1.4.0-beta2-b77, mixed mode)

      Two or more group references in a replacement string, with no characters between
      them causes appendReplacement to ignore the $ of the second group ref.

      The following code illustrates the problem. It should print

        Swap all: 123 = first, 456 = second
        Swap one: 123 = first, second = 456

      Instead it prints

        Swap all: 1232first, 4562second
        Swap one: 1232first, second = 456

      The code:

      import java.util.regex.*;

      public class RegTest {
      public static void main(String [] argv) {
      Pattern p = Pattern.compile("([a-z]+)( *= *)([0-9]+)");
      String s1 = "Swap all: first = 123, second = 456";
      String s2 = "Swap one: first = 123, second = 456";
      String r = "$3$2$1";

      Matcher m;

      m = p.matcher(s1);

      System.out.println(m.replaceAll(r));

      m = p.matcher(s2);

      if (m.find()) {
      StringBuffer sb = new StringBuffer();
      m.appendReplacement(sb, r);
      m.appendTail(sb);
      System.out.println(sb);
      }
      }
      }
      (Review ID: 130880)
      ======================================================================

            mmcclosksunw Michael Mccloskey (Inactive)
            nthompsosunw Nathanael Thompson (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: