[Python-Dev] iterator support for SRE?
Guido van Rossum
guido@python.org
Wed, 24 Oct 2001 23:35:56 -0400
> a common wish for SRE is a variant of findall that returns
> all match objects, instead of the strings.
>
> recently, I realized that the internal scanner type (originally
> added to speed up Python versions of findall/sub/split) can
> be wrapped in an iterator, allowing the user to loop over all
> matches.
>
> >>> import re
> >>> p = re.compile(somepattern)
> >>> for match in iter(p.scanner(somestring).search, None):
> >>> print match.groups()
>
> how about adding a "finditer" method, which takes care of
> the low-level setup:
>
> >>> for match in p.finditer(somestring):
> >>> print match.groups()
>
> or should we just make the scanner factory an official part
> of the SRE interface?
Or both? The scanner interface seems vaguely useful as a low-level
tool to create other higher-level variants; but the finditer() call
seems to nicely capture a common case.
--Guido van Rossum (home page: http://www.python.org/~guido/)