haxe icon indicating copy to clipboard operation
haxe copied to clipboard

clarify EReg.matched spec regarding capture groups

Open nadako opened this issue 4 years ago • 3 comments

So currently the doc for EReg.matched says this:

The index n corresponds to the n-th set of parentheses in the pattern of this 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));
}

nadako avatar Mar 17 '20 09:03 nadako

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? :)

nadako avatar Mar 17 '20 09:03 nadako

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.

nadako avatar Mar 17 '20 09:03 nadako

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.

lublak avatar Apr 05 '22 08:04 lublak