How to find overlapping matches with a regexp?

前端 未结 4 1604
悲&欢浪女
悲&欢浪女 2020-11-22 01:31
>>> match = re.findall(r\'\\w\\w\', \'hello\')
>>> print match
[\'he\', \'ll\']

Since \\w\\w means two characters, \'he\' and \'l

4条回答
  •  迷失自我
    2020-11-22 02:20

    Am no regex expert but I would like to answer my similar question.

    If you want to use a capture group with the lookahead:

    example regex: (\d)(?=.\1)

    string: 5252

    this will match the first 5 as well as the first 2

    The (\d) is to make a capture group, (?=\d\1) is to match any digit followed by the capture group 1 without consuming the string, thus allow overlapping

提交回复
热议问题