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

explain delimitation of $g syntax in java.util.regex.Matcher.appendReplacement

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • None
    • 1.4.0
    • docs
    • x86
    • windows_2000



      Name: nt126004 Date: 01/02/2002


      FULL PRODUCT VERSION :
      java version "1.4.0-beta3"
      Java(TM) 2 Runtime Environment, Standard Edition (build
      1.4.0-beta3-b84)
      Java HotSpot(TM) Client VM (build 1.4.0-beta3-b84, mixed mode)



      FULL OPERATING SYSTEM VERSION :
      Microsoft Windows 2000 [Version 5.00.2195] SP2



      A DESCRIPTION OF THE PROBLEM :
      In the javadoc for java.util.regex.Matcher.appendReplacement
      http://java.sun.com/j2se/1.4/docs/api/java/util/regex/Matcher.html#appendReplacement(java.lang.StringBuffer,%20java.lang.String)

      the explanation does not describe how g is delimited in $g.

      For example, I found that $99 was parsed as $9, so I thought
      that g was limited to a single digit. Later I happened to
      look at the code, and this appears to be incorrect: numerals
      are added only if the result does not exceed the number of
      groups in the pattern.

      1. This non-obvious behavior should at least be documented.

      2. I suggest there should be flag which causes the matcher
      to throw an exception instead when the numeral exceeds the
      groupCount. setGroupCountLimitsParse(boolean) or something.

      3. There should be an (alternate) syntax which provides an
      explicit delimiter. A problematic situation arises when the
      following characters in the template are digits. For
      example, suppose that $1 is bound to the hour, say 12, and I
      want to produce military time of 1200hours. Writing
      $100hours won't work if there are at least 10 groups, since
      it will be parsed as $10 rather than as $1. (If there is
      another workaround for this case that I don't see, it needs
      to be explained.)

      This bug can be reproduced always.
      (Review ID: 137481)
      ======================================================================

            shommel Scott Hommel (Inactive)
            nthompsosunw Nathanael Thompson (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: