Python re.findall() is not working as expected

前端 未结 4 1718
清酒与你
清酒与你 2020-12-06 16:45

I have the code:

import re
sequence=\"aabbaa\"
rexp=re.compile(\"(aa|bb)+\")
rexp.findall(sequence)

This returns [\'aa\']

4条回答
  •  情书的邮戳
    2020-12-06 17:42

    I do not understand why you use + - it means 0 or 1 occurrence, and is usually used when you want find string with optional inclusion of substring.

    >>> re.findall(r'(aa|bb)', 'aabbaa')
    ['aa', 'bb', 'aa']
    

    work as expected

提交回复
热议问题