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

java.util.regex.Pattern successfully compiles back references to unexisting capturing groups

XMLWordPrintable

    • b75
    • x86
    • windows_xp
    • Verified

      java.util.regex.Pattern successfully compiles back references to unexisting capturing groups. E.g.

      "\\1"
      "(a)\\2"
      "((a))\\3"

      etc. Problem is reproducible on both 6.0 and 5.0 did not check previous releases.

      JDK is behaving as if there were one more group than should be.

      How to reproduce:
      ----------------
      Compile and run the following code

      import java.util.regex.*;

      public class Test {

          public static void doTest(String pattern) {
              System.out.println("");

              try {
                  Pattern.compile(pattern);
                  System.out.println("Successfuly compiled pattern " + pattern);
              } catch (PatternSyntaxException pse) {
                  System.out.println("Failed to compile pattern " + pattern);
                  System.out.println(pse);
              }
          }

          public static void main(String[] args) {
              doTest("\\1");

              doTest("(a)\\2");
              doTest("\\2");

              doTest("((a))\\3");
              doTest("(a)\\3");
              doTest("\\3");
          }
      }

      Sample output:
      --------------
      C:\>"Z:\Links\java\re\jdk\1.5.0_04\promoted\fcs\b05\binaries\windows-i586\bin\ja
      va.exe" -showversion Test
      java version "1.5.0_04"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_04-b05)
      Java HotSpot(TM) Client VM (build 1.5.0_04-b05, mixed mode)


      Successfuly compiled pattern \1

      Successfuly compiled pattern (a)\2

      Failed to compile pattern \2
      java.util.regex.PatternSyntaxException: No such group yet exists at this point i
      n the pattern near index 1
      \2
       ^

      Successfuly compiled pattern ((a))\3

      Failed to compile pattern (a)\3
      java.util.regex.PatternSyntaxException: No such group yet exists at this point i
      n the pattern near index 4
      (a)\3
          ^

      Failed to compile pattern \3
      java.util.regex.PatternSyntaxException: No such group yet exists at this point i
      n the pattern near index 1
      \3
       ^

      C:\>"S:\jdk_promotions\JDK6.0\b68\binaries\windows-i586\jdk1.6.0\bin\java.exe" -
      showversion Test
      java version "1.6.0-rc"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.6.0-rc-b68)
      Java HotSpot(TM) Client VM (build 1.6.0-rc-b68, mixed mode)


      Successfuly compiled pattern \1

      Successfuly compiled pattern (a)\2

      Failed to compile pattern \2
      java.util.regex.PatternSyntaxException: No such group yet exists at this point i
      n the pattern near index 1
      \2
       ^

      Successfuly compiled pattern ((a))\3

      Failed to compile pattern (a)\3
      java.util.regex.PatternSyntaxException: No such group yet exists at this point i
      n the pattern near index 4
      (a)\3
          ^

      Failed to compile pattern \3
      java.util.regex.PatternSyntaxException: No such group yet exists at this point i
      n the pattern near index 1
      \3

            sherman Xueming Shen
            idergali Ilya Dergalin (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: