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

java.util.regex.Pattern: special group constructs shouldn't capture text

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Fixed
    • Icon: P4 P4
    • 1.4.0
    • 1.4.0
    • core-libs
    • beta2
    • generic
    • generic



      Name: bsC130419 Date: 06/13/2001


      java version "1.4.0-beta"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta-b65)
      Java HotSpot(TM) Client VM (build 1.4.0-beta-b65, mixed mode)


      As the sample code output below shows, any text that is
      matched by an independent group or a conditional group is
      automatically captured. I think this is incorrect, for
      two reasons. First, it isn't how these constructs work in
      Perl, where they first appeared, so anyone who is already
      familiar with them will be tripped up by this unexpected
      behavior. Second, while the regex author can choose to
      capture any part of an expression by enclosing it in
      parentheses, there is no way to tell these constructs NOT
      to capture; you're taking away some of the author's control
      over the matching process, for no good reason.

      ----------------------- sample code ---------------------------

      import java.util.regex.*;

      public class CaptureTest
      {
        Pattern pattern;
        Matcher matcher;
        
        public CaptureTest()
        {
          // independent group
          tryit("x+(?>y+)z+");

          // conditional
          tryit("x+(?(?=y)y+|z+)");

          // non-capturing group, for comparison
          tryit("x+(?:y+)z+");
        }
        
        public void tryit(String regex)
        {
          System.out.println();
          System.out.println(regex);
          try
          {
            pattern = Pattern.compile(regex);
            matcher = pattern.matcher("xxxyyyzzz");
            if (matcher.find())
            {
              System.out.println(matcher.group(1));
            }
          }
          catch (Exception ex)
          {
            System.out.println("error: " + ex.getMessage());
          }
        }

        public static void main(String[] argv)
        {
          new CaptureTest();
        }
      }

      ------------------------- output -----------------------------
      $ java CaptureTest

      x+(?>y+)z+
      yyy

      x+(?(?=y)y+|z+)
      yyy

      x+(?:y+)z+
      error: No group 1
      (Review ID: 126515)
      ======================================================================

            mmcclosksunw Michael Mccloskey (Inactive)
            bstrathesunw Bill Strathearn (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: