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

Denis Koroskin 2korden at gmail.com
Thu Feb 19 08:35:51 PST 2009


On Thu, 19 Feb 2009 19:00:41 +0300, Andrei Alexandrescu  
<SeeWebsiteForEmail at erdani.org> wrote:

[snip]
>> This is a little example of managing groups in Python:
>>
>>>>> import re
>>>>> data = ">hello1 how are5 you?<"
>>>>> patt = re.compile(r".*?(hello\d).*?(are\d).*")
>>>>> patt.match(data).groups()
>> ('hello1', 'are5')
>
> 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 would expect that to be

foreach (/*Capture */ i; 0 .. iter.engine.captures)
     writeln(i);

>> (notes that here all groups are found eagerly. If you want a lazy  
>> matching in Python you have to use re.finditer() or  
>> matchobj.finditer()).
>>  I may like a syntax similar to this, where opIndex() allows to find  
>> the matched group:
>>
>>>>> patt.match(data)[0]
>> 'hello1'
>>>>> patt.match(data)[1]
>> 'are5'
>
> No go due to confusions with random-access ranges.
>

Why iter.capture[0] and iter.capture[1] aren't good enough?
How are they different from iter.engine.captures[0] and  
iter.engine.captures[1]?

Why it is a no go if you access iter.captures as a random-access range?

I'm sorry if these are dumb questions, but the code you've shown is a bit  
confusing (these iter.engine.captures and iter.captures).

>
> Andrei



More information about the Digitalmars-d mailing list