Regex with no 2 consecutive a's and b's

Viewed 3153

I have been trying out some regular expressions lately. Now, I have 3 symbols a, b and c.

I first looked at a case where I don't want 2 consecutive a's. The regex would be something like:

((b|c + a(b|c))*(a + epsilon)

Now I'm wondering if there's a way to generalize this problem to say something like:

A regular expression with no two consecutive a's and no two consecutive b's. I tried stuff like:

(a(b|c) + b(a|c) + c)* (a + b + epsilon)

But this accepts inputs such as"abba" or "baab" which will have 2 consecutive a's (or b's) which is not what I want. Can anyone suggest me a way out?

3 Answers
Related