Regex backreference findall not working

前端 未结 1 1050
情书的邮戳
情书的邮戳 2020-12-11 09:25

I have recently been using regexes in a program. In this program I used them to find words in a list of words that matched a certain RE. However, when i tried backreferencin

相关标签:
1条回答
  • 2020-12-11 09:52

    The re.findall only returns captured values captured with capturing groups inside the regex pattern.

    Use re.finditer that will keep the zeroth group (the whole match):

    import re
    p = re.compile(r'[abcgr]([a-z])\1[ldc]')
    s = "reel reed have that with this they"
    print([x.group(0) for x  in p.finditer(s)])
    

    See the IDEONE demo

    0 讨论(0)
提交回复
热议问题