If I have a string and I need to write a regex pattern to find match only for a
and aa
, but not more than 2 consecutive a
. For instance, baaab
is not a match because 3 a
appear together, but abaa
is a match. Can I write a regex pattern for this purpose?
Here are something that I tried:
pattern = 'a{1,2}'
, but this pattern also matches'baaab'
.pattern = 'a{1,2}[^a]'
, but it failed to match'abaa'
pattern = 'a{1,2}[^a]?'
, but it also matches'baaab'
So what's the correct pattern I should use?