Taken from
HOWTO regular expression
span () returns both the start and end indexes in a single tuple. Since the matching method only checks to see if RE matches the beginning of the line, start () will always be zero. However, the RegexObject search method instances look at the string, so the match may not start from scratch in this case.
>>> p = re.compile('[az]+') >>> print p.match('::: message') None >>> m = p.search('::: message') ; print m <re.MatchObject instance at 80c9650> >>> m.group() 'message' >>> m.span() (4, 11)
Combine this with:
The finditer () method is also available in Python 2.2, returning a sequence of MatchObject instances as an iterator.
>>> p = re.compile( ... ) >>> iterator = p.finditer('12 drummers drumming, 11 ... 10 ...') >>> iterator <callable-iterator object at 0x401833ac> >>> for match in iterator: ... print match.span() ... (0, 2) (22, 24) (29, 31)
you can do something in order
for match in re.finditer(r'[az]', 'a1b2c3d4'): print match.span()
gone 30 Oct '08 at 14:16 2008-10-30 14:16
source share