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.

Author rhettinger
Recipients HaujetZhao, ezio.melotti, mrabarnett, rhettinger, serhiy.storchaka
Date 2020-11-23.19:46:36
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1606160796.28.0.505719676894.issue42448@roundup.psfhosted.org>
In-reply-to
Content
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
History
Date User Action Args
2020-11-23 19:46:36rhettingersetrecipients: + rhettinger, ezio.melotti, mrabarnett, serhiy.storchaka, HaujetZhao
2020-11-23 19:46:36rhettingersetmessageid: <1606160796.28.0.505719676894.issue42448@roundup.psfhosted.org>
2020-11-23 19:46:36rhettingerlinkissue42448 messages
2020-11-23 19:46:36rhettingercreate