Home >Backend Development >Python Tutorial >Why Doesn't `re.findall` Return Overlapping Regex Matches, and How Can Lookahead Assertions Solve This?
Problem:
When using re.findall to match a regular expression pattern, why does it not retrieve all overlapping matches? For instance, in the string "hello," why does the regex r'ww' only match "he" and "ll" but not "el" and "lo"?
Answer:
By default, re.findall does not yield overlapping matches. To achieve this, employ a lookahead assertion, a powerful regex feature.
Solution:
# Using a lookahead assertion matches = re.findall(r'(?=(\w\w))', 'hello') # Output: ['he', 'el', 'll', 'lo']
The (?=...) construct in the regex is a lookahead assertion. It matches if the specified pattern appears immediately after the current position, but it does not consume any characters from the string. In this case, it identifies all two-character sequences ("ww") in "hello" without consuming any characters.
Explanation:
The above is the detailed content of Why Doesn't `re.findall` Return Overlapping Regex Matches, and How Can Lookahead Assertions Solve This?. For more information, please follow other related articles on the PHP Chinese website!