re.findall documentation wrong?

Inyeol Lee inyeol.lee at siimage.com
Fri May 23 16:49:02 EDT 2003


On Fri, May 23, 2003 at 10:30:59PM +0300, Antti Kaihola wrote:
> The current re module documentation says:
> 
> findall(pattern, string)
>     Return a list of all non-overlapping matches of pattern in string.
>     If one or more groups are present in the pattern, return a list of
>     groups; this will be a list of tuples if the pattern has more than
>     one group. Empty matches are included in the result. New in version
>     1.5.2.
> 
> However, re.findall() or re.compile(<regexp>).findall() refuse to return 
> lists of anything but strings.

re.findall behaves as documented.

Python 2.3b1 (#2, Apr 28 2003, 20:05:02) 
[GCC 2.95.3 20010315 (release)] on sunos5
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> 
>>> # Return a list of all non-overlapping matches.
... re.compile(r"\d+").findall("abc 123 def 456")
['123', '456']
>>> # Return a list of groups.
... re.compile(r"\w+ (\d+)").findall("abc 123 def 456")
['123', '456']
>>> # Return a list of tuples if the pattern has more than one group.
... re.compile(r"(\w+) (\d+)").findall("abc 123 def 456")
[('abc', '123'), ('def', '456')]
>>> 

I think you are confused with 'group' attribute of match object. To get
match object (which has group attr), use re.finditer instead.

Inyeol





More information about the Python-list mailing list