Computer Science Stack Exchange is a question and answer site for students, researchers and practitioners of computer science. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am getting confused by the regular expression $(a\mid b)^*$ as it for sure matches $aab$ and $ab$.

Does $(a\mid b)^*$ also match strings like $aa$, $aaaa$, $bb$ or $bbb$, that is those that use only $a$ or $b$?

share|cite|improve this question

migrated from Nov 9 '12 at 6:15

This question came from our site for theoretical computer scientists and researchers in related fields.

Wow, please use standard terminology. I don't get what the problem here is; isn't this clear from the definition? You have looked at the definition of regular expressions, in particular $\mid$ and $^*$, right? – Raphael Nov 9 '12 at 7:56

Yes: $(a\mid b)^*$ is the concatenation of any number of $a$'s and $b$'s, so the expression matches $a^n$ and $b^n$.

As you can read more about on Wikipedia, the vertical bar $\mid$ is the boolean "or" relation (one or the other must be included in the regular expression), while the asterisk $^*$ indicates that the preceding element is repeated zero or more times. Hence:

$(a\mid b)^*$ is equivalent to $(a\;|\;b)(a\;|\;b)(a\;|\;b)\cdots$ repeated any number of times (even zero).

For each term, you can select either $a$ or $b$, so strings like $aa$, $aaaa$, $bb$ and $bbb$ are valid. In particular, this expression matches the empty string $\epsilon$, which has zero $a$'s and zero $b$'s.

share|cite|improve this answer
Your $\equiv$ is incorrect, it would imply an infinite sequence of characters. – phant0m Nov 9 '12 at 15:00
Fixed definition of Kleene star. – JeffE Nov 9 '12 at 16:20

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.