haxe
haxe copied to clipboard
clarify EReg.matched spec regarding capture groups
So currently the doc for EReg.matched
says this:
The index
n
corresponds to the n-th set of parentheses in the pattern ofthis
EReg. If no such sub-group exists, the result is unspecified.
But it's not immediately clear what "no such sub-group exists" means exactly. I assume it means that the group was not defined in the regex. But it's a bit confusing in case the group was defined but was not matched.
I.e. is it safe to rely on the following to consistently trace an empty string?
var r = ~/(a)?b/;
if (r.match("b")) {
trace(r.matched(1));
}
is it safe to rely on the following to consistently trace an empty string?
Looks like it isn't because at least on JS that returns undefined
. Maybe null
could be a spec then? :)
And looks like we indeed test it for null
: https://github.com/HaxeFoundation/haxe/blob/3352cab1d38992ef317d110151783c50fd138b41/tests/unit/src/unit/TestEReg.hx#L23
That makes it a documentation and type-hinting (Null<String>
instead of String
) issue.
I had the same question. Just when you want to test if a group applies: Example: https://regex101.com/r/RvD4XP/1. Would it be practical to test for zero. If most targets return zero anyway, a documentation about it would be really great.