Inconsistent behaviour for unmatched subpatterns in Regex::Matchdata
#12806
Labels
kind:bug
A bug in the code. Does not apply to documentation, specs, etc.
status:discussion
topic:stdlib:text
A regex match with unused optional subpatterns leads to inconsistent and apparently unexpected behaviour when querying information about those subpatterns in
Regex::Matchdata
.The
#[]?
and#[]
methods handle this condition explicitly, returningnil
or raisingIndexError
orKexError
with an error message indicating the problem. A minor downside is that the error type does not distinquish between index out of range and an unused subpattern, but that's probably okay. And you can tell from the error message which error it is.#begin
and#end
raise an unspecificNilAssertionError
. This should be improved. I think it makes sense to raiseIndexError
like#[]
does. I would also suggest non-raising variants#begin?
and#end?
which returnnil
if the subpattern was unmatched (or index is out of range).#byte_begin
and#byte_end
both return-1
which should be considered a bug. This is clearly incorrect behaviour.Error handling for these methods should be like
#begin
and#end
raisingIndexError
. The additional non-raising variants would also make sense.The text was updated successfully, but these errors were encountered: