import re
pattern = r'faf'
string = 'fafaf'
print(len(re.findall(pattern, string)))
it is giving the answer as 1, but required answer is 2
import re
pattern = r'faf'
string = 'fafaf'
print(len(re.findall(pattern, string)))
it is giving the answer as 1, but required answer is 2
You want to use a positive lookahead r"(?=(<pattern>))"
to find overlapping patterns:
import re
pattern = r"(?=(faf))"
string = "fafaf"
print(len(re.findall(pattern, string)))
You can test regexes here: https://regex101.com/r/3FxCok/1