I need a regex which matches strings of consecutive a
and b
, e.g.:
ababa
bab
Edge case (smallest):
ab
ba
(No upper limit.)
...and shouldn't match:
abba
bbab
bbaabb
I've tried several regex but this one is kind of tricky. Can you throw me any hints?
My tries:
(a|b)+
(ab|ba)*(aba|bab)+
This one gets really close! http://www.regexr.com/38lqg
If you want to find matches within a text (potentially several words per line):
\b(((ab)+a?)|((ba)+b?))\b
\b
is for word boundary.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With