MatchResult support for named groups

Cay Horstmann cay.horstmann at gmail.com
Sat Dec 5 07:43:10 UTC 2020


Hi Stuart,

1: If there is no match at all, then results yields the empty stream. I 
don't think anything else is required.

2/3: I wrote a fair number of regex patterns in Java, ever since they 
appeared in 2002. I can say with confidence that I never once used 
hitEnd/requireEnd, or seen it used. I note they occur in one file in the 
JDK 11 source, in the Scanner class. But not in a loop that fetches all 
matches, but after a single call to find or lookingAt. I think these are 
exceedingly uncommon.

In contrast, looping over matcher.find() and extracting groups is 
common, and named groups are a best practice 
(https://eslint.org/docs/rules/prefer-named-capture-group).

Cheers,

Cay

Il 04/12/2020 19:53, Stuart Marks ha scritto:
> Hi Cay,
> 
> Thanks for mentioning this. It's good to know that adding this provides 
> value to people who are actually trying to use this stuff (as opposed to 
> adding stuff merely for the sake of completeness, as often seems to arise).
> 
> I've added some notes to JDK-8065554.
> 
> Looking at this more closely, it seems to me that MatchResult ought to 
> include more match-result-related information that's currently only in 
> Matcher, namely:
> 
> 1. whether there was a match at all
> 2. hitEnd
> 3. requireEnd
> 
> If you have any thoughts on these, please let me know.
> 
> s'marks
> 
> On 12/2/20 2:53 AM, Cay Horstmann wrote:
>> Hello, I'd like to raise awareness for
>>
>> https://bugs.openjdk.java.net/browse/JDK-8180352
>> https://bugs.openjdk.java.net/browse/JDK-8072984
>> https://bugs.openjdk.java.net/browse/JDK-8065554
>>
>> These all ask for MatchResult.group(String name). What they don't 
>> mention is that this is more urgent in light of the methods
>>
>> Stream<MatchResult> Matcher.results() // 
>> https://bugs.openjdk.java.net/browse/JDK-8071479
>> Stream<MatchResult> Scanner.findAll(Pattern pattern) // 
>> https://bugs.openjdk.java.net/browse/JDK-8072722
>>
>> In particular, Matcher.results() seems a cleaner way of collecting 
>> match results than calling while (matcher.find()).
>>
>> But then MatchResult needs to support the same queries that Matcher 
>> provides. I believe the only missing one is group(String name).
>>
>> Cheers,
>>
>> Cay
>>
>> NB. There are related requests that ask for finding group names in 
>> patterns, or for correlating group names and numbers. I have formed no 
>> opinion on their merits.
>>

-- 

Cay S. Horstmann | http://horstmann.com | mailto:cay at horstmann.com


More information about the core-libs-dev mailing list