suggestion for module re

Fredrik Lundh fredrik at
Mon Oct 22 22:44:04 CEST 2001

Jose Sebrosa wrote:
> Anyway, it still remains the need of a function to travel through a string and
> collect all the matches in one single object, *while preserving the names*.

if you pass a callable object to the sub method, that object will
be called with a match object for each match in the string.

for example:

import re

class Scanner:
    def __init__(self, pattern):
        self.pattern = re.compile(pattern)
    def parse(self, string):
        self.result = []
        self.pattern.sub(self.handler, string)
        return self.result
    def handler(self, match):
        # can be overridden
        return "" # must return string

>>> scanner = Scanner('(?P<a>a|A)(?P<b>b|B)')
>>> for item in scanner.parse('aBAb'):
>>>    print item
{'b': 'B', 'a': 'a'}
{'b': 'b', 'a': 'A'}

this works with 1.5.2 and newer.  to get custom behaviour,
override the "handler" method.

in Python 2.2, you can use the undocumented scanner object,
together with an iterator, to loop over every match:

>>> import re
>>> p = re.compile('(?P<a>a|A)(?P<b>b|B)')
>>> for match in iter(p.scanner('aBAb').search, None):
>>>     print match.groupdict()
{'a': 'a', 'b': 'B'}
{'a': 'A', 'b': 'b'}


<!-- (the eff-bot guide to) the python standard library:

More information about the Python-list mailing list