findall
doesn't yield overlapping matches by default. This expression does however:
>>> re.findall(r'(?=(ww))', 'hello')
['he', 'el', 'll', 'lo']
Here (?=...)
is a lookahead assertion:
(?=...)
matches if ...
matches next, but doesn’t consume any of the
string. This is called a lookahead assertion. For example,
Isaac (?=Asimov)
will match 'Isaac '
only if it’s followed by 'Asimov'
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…