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

Regex does not match pattern correctly, only 2 out of 3 groups generated

    XMLWordPrintable

    Details

      Description

      ADDITIONAL SYSTEM INFORMATION :
      Ubuntu
      $ lsb_release -r
      Release: 18.04
      $ java -version
      openjdk version "14" 2020-03-17
      OpenJDK Runtime Environment (build 14+36-1461)
      OpenJDK 64-Bit Server VM (build 14+36-1461, mixed mode, sharing)


      A DESCRIPTION OF THE PROBLEM :
      Regex doesn't match the pattern "X(@([^@]+)@)" correctly. There should be three groups generated and only the first two are. The third is missing.

      REGRESSION : Last worked in version 14

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      0. Open a shell
      1. Save the code below in a file called: REBug.java
      2. Compile: javac REBug.java
      3 Run the code in a shell: java REBug


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      m.groupCount()=2 <- should be 3
      m.group(0)=X@b@
      m.group(1)=@b@
      m.group(2)=b <- missing (bug)
      p: {a=X@b@}

      ACTUAL -
      m.groupCount()=2
      m.group(0)=X@b@
      m.group(1)=@b@
      p: {a=X@b@}


      ---------- BEGIN SOURCE ----------
      public class REBug {

          private static java.util.regex.Pattern p = java.util.regex.Pattern.compile(
              "X(@([^@]+)@)"
            // 1 2
          );

          public static String[] workaround(String pat) {
              String[] sa = new String[3];
              sa[0] = pat;
              sa[1] = pat.substring(1);
              sa[2] = pat.substring(2, pat.length() - 1);
              return sa;
          }

          public static void main(String[] args) throws Exception {
              String pat = "X@b@";
              java.util.regex.Matcher m = p.matcher(pat);
              //if (m.find()) { // same result
              //if (m.lookingAt()) { // same result
              if (m.matches()) {
      System.err.println("# bug:");
      System.err.println("m.groupCount()="+m.groupCount());
                  for (int i = 0; i < m.groupCount(); ++i) {
      System.err.println("m.group("+i+")="+m.group(i));
                  }
              }

      System.err.println("# workaround:");
              String[] sa = workaround(pat);
      System.err.println("sa.length="+sa.length);
              for (int i = 0; i < sa.length; ++i) {
      System.err.println("sa["+i+"]="+sa[i]);
              }
          }

      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      See the workaround function provided (quick and dirty).

      FREQUENCY : always


        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: