regex question

rusi rustompmody at gmail.com
Fri Jul 29 10:53:53 EDT 2011


Can someone throw some light on this anomalous behavior?

>>> import re
>>> r = re.search('a(b+)', 'ababbaaabbbbb')

>>> r.group(1)
'b'
>>> r.group(0)
'ab'
>>> r.group(2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: no such group

>>> re.findall('a(b+)', 'ababbaaabbbbb')
['b', 'bb', 'bbbbb']

So evidently group counts by number of '()'s and not by number of
matches (and this is the case whether one uses match or search). So
then whats the point of search-ing vs match-ing?

Or equivalently how to move to the groups of the next match in?

[Side note: The docstrings for this really suck:

>>> help(r.group)
Help on built-in function group:

group(...)

>>>



More information about the Python-list mailing list