Reputation: 23322
I have a string aaab
. I want a Python expression to match aa
, so I expect the regular expression to return aa
and aa
since there are two ways to find substrings of aa
.
However, this is not what's happening.
THis is what I've done
a = "aaab"
b = re.match('aa', a)
Upvotes: 1
Views: 34
Reputation: 1166
To generalize @stribizhev solution to match one or more of character a
: (?=(a{1,}))
For three or more: (?=(a{3,}))
etc.
Upvotes: 0
Reputation: 627101
You can achieve it with a look-ahead and a capturing group inside it:
(?=(a{2}))
Since a look-ahead does not move on to the next position in string, we can scan the same text many times thus enabling overlapping matches.
See demo
import re
p = re.compile(r'(?=(a{2}))')
test_str = "aaab"
print(re.findall(p, test_str))
Upvotes: 1