Search code examples
python-3.xfindall

re.findall groups working mechanism in python3.6


I am learning python re module. and want to know what's the working mechanism of re.findall() function.

I already viewed official documents of python, I still not understand what's the re.findall groups working mechanism. python3.6 findall() document..

I am confused with the below code:

import re
#output: ['def']
print(re.findall('\w+\s+(\w+)','abc def'))

I expect the output of re.findall('\w+\s+(\w+)','abc def') to be [('abc def','def')], but the actual output is ['def'].


Solution

  • For every match, findall() returns all capture groups [1].

    Your regex matches one substring, abc def. The regex has a single capture group, so that's what gets returned.

    If you want to return both the entire match and the second word of the match, you need to have two capture groups, like so:

    >>> print(re.findall('(\w+\s+(\w+))','abc def'))
    [('abc def', 'def')]
    

    [1] If there are no capture groups, the entire match is returned.