Is str ~ regex the root of all evil, or the leaf of all good?

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Thu Feb 19 10:04:21 PST 2009


bearophile wrote:
> Andrei Alexandrescu:
>> auto data = ">hello1 how are5 you?<";
>> auto iter = match(data, regex(r".*?(hello\d).*?(are\d).*"));
>> foreach (i; 0 .. iter.engine.captures)
>>      writeln(iter.capture[i]);
> 
> I don't understand that.
> 
> What's the purpose of ".engine"?

It's the regex engine that has generated the match. I coded that wrong 
in two different ways, it should have been:

foreach (i; 0 .. iter.captures)
       writeln(iter.capture(i));

> "captures" may be better named "ngroups" or "ncaptures", or you may just use the .len/.length attribute in some way.

"Capture" is the traditional term as far as I understand. I can't use 
.length because it messes up with range semantics. "len" would be too 
confusing. "ncaptures" is too cute. Nobody's perfect :o).

> foreach (i, group; iter.groups)
>     writeln(i " ", group);
> 
> "group" may be a struct that defines toString and can be cast to string, and also keeps the starting position of the group into the original string.

That sounds good.


Andrei



More information about the Digitalmars-d mailing list