How to find overlapping matches with a regexp?

前端 未结 4 1608
悲&欢浪女
悲&欢浪女 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:16

    You can use the new Python regex module, which supports overlapping matches.

    >>> import regex as re
    >>> match = re.findall(r'\w\w', 'hello', overlapped=True)
    >>> print match
    ['he', 'el', 'll', 'lo']
    

提交回复
热议问题