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

Multiple quantifiers such as “a{2}{3}” are misinterpreted

XMLWordPrintable

    • x86_64
    • windows
    • Verified

      ADDITIONAL SYSTEM INFORMATION :
      Windows 11 Pro 24H2
      The version reported by “javac --version” and “java --version” is 23.0.2.


      A DESCRIPTION OF THE PROBLEM :
      Other prominent regular expression engines do not accept patterns that contain multiple quantifiers of type “a{2}{3}”. However, the java.util.regex package appears to accept this pattern, interpreting it as “a{2}”. The “{3}” is silently disregarded.

      One library (in C++) interprets “a{2}{3}” as “(a{2}){3}”, or “a{6}”, which seems logical, though this is atypical. Another library (in Rust) interprets “a{2}{3}” as “aa” followed by “{”, “3” and “}”.

      Most major engines produce errors in this situation. Java should likely do the same.


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the sample code that searches for “a{2}{3}” in “aa”.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      According to general practice, the pattern should be rejected. The Pattern.compile should throw an exception.
      ACTUAL -
      The “a{2}{3}” pattern is accepted as “a{2}”, and the program prints the found segment: “0..2”.

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

      class MultipleQuantifiersDefect
      {
          public static void main( String[] args)
          {
              Pattern pattern = Pattern.compile( "a{2}{3}");
              Matcher matcher = pattern.matcher( "aa");

              while( matcher.find( ))
              {
                  System.out.printf( "%d..%d %n", matcher.start( ), matcher.end( ));
              }
          }
      }

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

            rgiulietti Raffaello Giulietti
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: