-
Enhancement
-
Resolution: Duplicate
-
P3
-
None
-
None
-
None
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 ifJDK-8071479 is implemented, the replacer function parameter taken by the methods added by that RFE should also use MatchResult2.
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
- duplicates
-
JDK-8065554 MatchResult should provide values of named-capturing groups
-
- Resolved
-
- relates to
-
JDK-8071479 Stream and lambdafication improvements to j.u.regex.Matcher
-
- Closed
-