This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: re.findall()
Type: Stage: resolved
Components: Regular Expressions Versions: Python 2.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: ezio.melotti, jmfauth, joncle
Priority: normal Keywords:

Created on 2010-03-20 17:20 by jmfauth, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (3)
msg101381 - (view) Author: jmf (jmfauth) Date: 2010-03-20 17:20
>>> sys.version
2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit
(Intel)]
>>> import re
>>> re.match("[-+]?[0-9]+[.]?[0-9]*([eE][-+]?[0-9]+)?", "1.23e-4").group()
1.23e-4
>>> re.search("[-+]?[0-9]+[.]?[0-9]*([eE][-+]?[0-9]+)?", "1.23e-4").group()
1.23e-4
>>> for e in re.finditer("[-+]?[0-9]+[.]?[0-9]*([eE][-+]?[0-9]+)?", "1.23e-4"):
        print e.group()
        
1.23e-4

but

>>> re.findall("[-+]?[0-9]+[.]?[0-9]*([eE][-+]?[0-9]+)?", "1.23e-4")
['e-4']
>>> 

It seems re.findall() does not like patterns containing parentheses.

>>> re.findall("[-+]?[0-9]+[.]?[0-9]*[eE][-+]?[0-9]+", "1.23e-4")
['1.23e-4']
>>> 
>>> re.findall("a(b)?", "ab")
['b']
>>> re.findall("ab?", "ab")
['ab']
>>>
msg101391 - (view) Author: Jon Clements (joncle) Date: 2010-03-20 19:53
Seems consistent to me:

.match, .search and .finditer return a MatchObject whose .group() return the *entire matched string*. If you use .group(1) you'll get similar results to .findall() which returns a list of (possibly of tuples) of the captured groupings.

This is the findall equivalent (using a non-capturing group):

>>> re.findall('a(?:b)?', 'ab')
['ab']
msg101399 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2010-03-20 21:21
What Jon said is correct, .group() is equivalent to .group(0) and returns the whole match. re.findall returns all the groups captured by each set of () as a list of strings (if there is 0 or 1 group) or a list of tuples (if there are more than 1).
History
Date User Action Args
2022-04-11 14:56:58adminsetgithub: 52432
2010-03-20 21:21:54ezio.melottisetstatus: open -> closed

nosy: + ezio.melotti
messages: + msg101399

resolution: not a bug
stage: resolved
2010-03-20 19:53:05jonclesetnosy: + joncle
messages: + msg101391
2010-03-20 17:20:59jmfauthcreate