Is it possible to construct a regex that matches a pattern multiple times?
For example searching for ff
in fff
would give two matches. Their starting position would be 0 and 1 respectively.
Is it possible to construct a regex that matches a pattern multiple times?
For example searching for ff
in fff
would give two matches. Their starting position would be 0 and 1 respectively.
Yes, it is possible. You can use positive lookahead for this.
>>> import re
>>> [m.start() for m in re.finditer(r'f(?=f)', 'fff')]
[0, 1]
Yes. Use findall(string[, pos[, endpos]])
Similar to the findall() function, using the compiled pattern, but also accepts optional pos and endpos parameters that limit the search region like for match().
i.e. Each time you will begin search from the m.start()
of the previous match + 1
.