Raymond Hettinger <raymond.hettin...@gmail.com> added the comment:

When groups are present in the regex, findall() returns the subgroups rather 
than the entire match:

    >>> mo = re.search('(12)+', '121212 and 121212')
    >>> mo[0]                  # Entire match
    '121212'
    >>> mo[1]                  # Group match
    '12'

To get the result you were looking for use a non-capturing expression:

    >>> re.findall('(?:12)+', '121212 and 121212')
    ['121212', '121212']

Also consider using finditer() which gives more fine grained control:

    >>> for mo in re.finditer('(12)+', '121212 and 121212'):
            print(mo.span())
            print(mo[0])
            print(mo[1])
            print()
        
    (0, 6)
    121212
    12

    (11, 17)
    121212
    12

----------
nosy: +rhettinger

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue42448>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to