Is str ~ regex the root of all evil, or the leaf of all good?
Andrei Alexandrescu
SeeWebsiteForEmail at erdani.org
Thu Feb 19 10:05:42 PST 2009
Denis Koroskin wrote:
> 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).
They're good. The code I posted was dumb. The "engine" thing does not
belong there, and "captures" should be indeed a random-access range.
Andrei
More information about the Digitalmars-d
mailing list