t at jollybox.de
Fri Jul 29 17:45:10 CEST 2011
On 29/07/11 16:53, rusi wrote:
> Can someone throw some light on this anomalous behavior?
>>>> import re
>>>> r = re.search('a(b+)', 'ababbaaabbbbb')
> 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 on built-in function group:
Pretty standard regex behaviour: Group 1 is the first pair of brackets.
Group 2 is the second, etc. pp. Group 0 is the whole match.
The difference between matching and searching is that match assumes that
the start of the regex coincides with the start of the string (and this
is documented in the library docs IIRC). re.match(exp, s) is equivalent
to re.search('^'+exp, s). (if not exp.startswith('^'))
Apparently, findall() returns the content of the first group if there is
one. I didn't check this, but I assume it is documented.
More information about the Python-list