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

Regular expression wrong start position of matching group

XMLWordPrintable

    • Cause Known
    • generic
    • generic

      FULL PRODUCT VERSION :
      java version "1.8.0_73"
      Java(TM) SE Runtime Environment (build 1.8.0_73-b02)
      Java HotSpot(TM) 64-Bit Server VM (build 25.73-b02, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      WIndows 10, online Java, etc.

      A DESCRIPTION OF THE PROBLEM :
      After matching the regular expression ab(?:a(c))*ac over the string abacacac,
      querying the start position of the matched group (c) returns 7, which is not possible since the c at position c is matched by the last c of the regular expression. And (?: ...) is consuming, so there is no look-ahead.


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Paste the following code into an online java IDE such as: https://www.compilejava.net/


      import java.util.regex.*;

      public class HelloWorld
      {
        public static void main(String[] args)
        {
          Pattern p = Pattern.compile("ab(?:a(c))*ac");
           Matcher m = p.matcher("abacacac");
           m.find();
           System.out.println(m.start(1));
        }
      }


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.util.regex.*;

      public class HelloWorld
      {
        public static void main(String[] args)
        {
          Pattern p = Pattern.compile("ab(?:a(c))*ac");
           Matcher m = p.matcher("abacacac");
           m.find();
           System.out.println(m.start(1));
        }
      }
      ---------- END SOURCE ----------

            sherman Xueming Shen
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: