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

MatchResult should support named capturing groups

    XMLWordPrintable

    Details

    • Type: Enhancement
    • Status: Closed
    • Priority: P3
    • Resolution: Duplicate
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: core-libs
    • Labels:
      None

      Description

      The Pattern and Matcher classes have been enhanced in Java SE 7 to support named capturing groups in addition to the original numbered capturing groups. The Matcher class has also been enhanced with new methods group(String), start(String), and end(String) -- the latter two in Java SE 8 -- to retrieve information about what was matched by the named capturing group. However, the MatchResult interface has not been enhanced to support named capturing groups. This is not surprising, as adding methods to an interface was an incompatible change prior to Java SE 8.

      There are a couple approaches possible:

      1. Add extension methods group(String), start(String), and end(String) as default methods to the MatchResult interface. The default implementations might throw IllegalArgumentException, which is thrown by the corresponding Matcher methods if no such named capturing group exists in the regex. Of course, Matcher's implementations will override the default implementations, and their behavior should remain unchanged.

      2. Add a sub-interface (e.g., MatchResult2) that extends MatchResult, adjust Matcher to implement MatchResult2, and adjust various methods that return MatchResult to return MatchResult2 instead. There are only a couple such methods: Scanner.match() and Matcher.toMatchResult(). When and if JDK-8071479 is implemented, the replacer function parameter taken by the methods added by that RFE should also use MatchResult2.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                smarks Stuart Marks
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: