Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't MatchResult have a method for named groups?

Tags:

java

regex

Matcher implements MatchResult, which "contains query methods used to determine the results of a match against a regular expression." Surprisingly, although group(), group(int) and groupCount() are available through the MatchResult interface, group(String) is not.

Is this an oversight, or was it left out Java 7 for compatibility reasons? If it's for backwards compatibility, couldn't that have been resolved with a default method in Java 8?

like image 617
shmosel Avatar asked Oct 19 '22 04:10

shmosel


1 Answers

This is because MatchResult interface has been added with Java 1.5, while group(String) is a Java 1.7 addition.

Adding group(String) to the interface would have been a breaking change, because any other implementation outside of Java's Matcher would not compile. Java designers generally avoid interface changes like that, so they did not do it.

A more interesting question is why they have not done it in Java 1.8, which allows specifying default implementations for interfaces. My guess is that there is no good default for such implementation, apart from throwing a "not implemented" exception.

like image 200
Sergey Kalinichenko Avatar answered Nov 03 '22 01:11

Sergey Kalinichenko