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

REGEX Class Pattern -- differences in group 0 with find, lookingAt, matches

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 1.4.0
    • 1.4.0
    • core-libs
    • beta2
    • x86
    • windows_98
    • Verified



      Name: bsC130419 Date: 05/31/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)


      A successful match using the matches() or lookingAt()
      method doesn't always update the start and end positions
      of the overall match (group 0). The result, as shown in
      the code sample below, is that a call to matcher.group()
      returns null--which, as I understand it, should never
      happen. The find() method always seems to update the
      positions properly.

      Looking at the code, I see that find() works by calling
      pattern.root.match(), while the other two methods call
      pattern.matchRoot.match(). Furthermore, the matchRoot
      object object starts out as an instance of LastNode, and
      in LastNode's match() method, the lines that set the
      overall start and end positions are commented out.

      -------------------------- PatternTest.java -----------------------

      import java.util.regex.*;
      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)


      A successful match using the matches() or lookingAt()
      method doesn't always update the start and end positions
      of the overall match (group 0). The result, as shown in
      the code sample below, is that a call to matcher.group()
      returns null--which, as I understand it, should never
      happen. The find() method always seems to update the
      positions properly.

      Looking at the code, I see that find() works by calling
      pattern.root.match(), while the other two methods call
      pattern.matchRoot.match(). Furthermore, the matchRoot
      object object starts out as an instance of LastNode, and
      in LastNode's match() method, the lines that set the
      overall start and end positions are commented out.

      -------------------------- PatternTest.java -----------------------

      import java.util.regex.*;

      public class PatternTest
      {
        public static void main(String[] argv)
        {
          Pattern pattern = Pattern.compile("(tes)ting");
          Matcher matcher = pattern.matcher("testing");

          if (matcher.find()) {
            System.out.println("\nfind:");
            System.out.println(matcher.group(0));
            System.out.println(matcher.group(1));
          }

          pattern = Pattern.compile("te(sti)ng");
          matcher = pattern.matcher("testing");

          if (matcher.lookingAt()) {
            System.out.println("\nlookingAt:");
            System.out.println(matcher.group(0));
            System.out.println(matcher.group(1));
          }

          pattern = Pattern.compile("test(ing)");
          matcher = pattern.matcher("testing");

          if (matcher.matches()) {
            System.out.println("\nmatches:");
            System.out.println(matcher.group(0));
            System.out.println(matcher.group(1));
          }

          pattern = Pattern.compile("te(sti)ng!");
          matcher = pattern.matcher("testing");

          if (matcher.lookingAt()) {
            System.out.println("\nlookingAt with bang:");
            System.out.println(matcher.group(0));
            System.out.println(matcher.group(1));
          }

          pattern = Pattern.compile("^test(ing)");
          matcher = pattern.matcher("testing");

          if (matcher.matches()) {
            System.out.println("\nmatches with caret:");
            System.out.println(matcher.group(0));
            System.out.println(matcher.group(1));
          }
        }
      }

      ------------------------------ output ---------------------------------

      find:
      testing
      tes

      lookingAt:
      null
      sti

      matches:
      null
      ing

      lookingAt with bang:
      testing
      sti

      matches with caret:
      testing
      ing

      (Review ID: 125324)
      ======================================================================

            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: