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

MatchResult produces StringIndexOutOfBoundsException for groups outside match

XMLWordPrintable

    • 21
    • b10
    • generic
    • generic
    • Verified

        ADDITIONAL SYSTEM INFORMATION :
        JDK 21ea31

        A DESCRIPTION OF THE PROBLEM :
        With the fix for JDK-8132995 MatchResult only stores the substring covering the match but does not account for capturing groups outside the match, i.e. within look-behind or look-ahead groups.

        The correct behavior would be not only consider the start and end of the match but the minimum start and the maximum end of all capturing groups.

        REGRESSION : Last worked in version 20.0.2

        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        Compile and run the attached program


        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        It should print

        match from 3 to 6: ABC
        prefix: 123
        suffix: 456
        match from 3 to 6: ABC
        prefix: 123
        suffix: 456

        ACTUAL -
        match from 3 to 6: ABC
        prefix: 123
        suffix: 456
        match from 3 to 6: ABC
        Exception in thread "main" java.lang.StringIndexOutOfBoundsException: Range [-3, 0) out of bounds for length 3
                at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:55)
                at java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:52)
                at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:213)
                at java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:210)
                at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:98)
                at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckFromToIndex(Preconditions.java:112)
                at java.base/jdk.internal.util.Preconditions.checkFromToIndex(Preconditions.java:349)
                at java.base/java.lang.String.checkBoundsBeginEnd(String.java:4861)
                at java.base/java.lang.String.substring(String.java:2830)
                at java.base/java.util.regex.Matcher$ImmutableMatchResult.group(Matcher.java:348)
                at CapturingGroups.printResult(CapturingGroups.java:27)
                at CapturingGroups.main(CapturingGroups.java:21)


        ---------- BEGIN SOURCE ----------
        import java.nio.CharBuffer;
        import java.util.Arrays;
        import java.util.regex.MatchResult;
        import java.util.regex.Matcher;
        import java.util.regex.Pattern;

        public class CapturingGroups {
          public static void main(String[] args) {
              char[] data = "123ABC456".toCharArray();
          
              Matcher m = Pattern.compile("(?<=(\\d{3})).*(?=(\\d{3}))").matcher(CharBuffer.wrap(data));
          
              if(!m.find()) throw new AssertionError();
          
              printResult(m);
          
              MatchResult mr = m.toMatchResult();
          
              Arrays.fill(data, '*');
          
              printResult(mr);
          }

          private static void printResult(MatchResult mr) {
              System.out.println("match from " + mr.start() + " to " + mr.end() + ": " + mr.group());
          
              System.out.println("prefix: " + mr.group(1));
              System.out.println("suffix: " + mr.group(2));
          }
        }

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

        FREQUENCY : always


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

                Created:
                Updated:
                Resolved: