# Regular expression for words over $\{0,1\}$ containing at least one 1

I had an exam on Theory of Computation, and one of the questions was to write down a regular expression for the language over $\{0,1\}$ consisting of all words containing at least one 1. My answer was:

$$0^* 1 0^* (0^* 1 0^*)^* \, .$$

My teacher gave me one mark out of 5, yet I can't find a possible string that will give false output here! His answer, which was $(0+1)^*1(0+1)^*$, was better, but mine works as well, I believe.

• Your answer is definitely not wrong. The 3rd 0* is a bit useless, but that's hardly a reason to penalize. Dec 7 '14 at 14:17
• Zaid, did you speak to your teacher after reading the answer here? Dec 15 '14 at 17:31
• @HendrikJan no he haven't showed up since then! Dec 17 '14 at 13:13

Your answer is correct. $0^*1$ matches the portion up to the first $1$. After that, any subsequent $1$s may have any number of $0$s between them.

There are slightly simpler regular expressions for the same language. For example,

$$0^*10^*(0^*10^*)^* \ \equiv\ 0^*10^*0^*(10^*)^*\ \equiv\ 0^*10^*(10^*)^*\,.$$

You could also observe that a string with at least one $1$ is formed of some number of initial $0$s, followed by the first $1$, followed by anything else, leading to $0^*1(0+1)^*\!$, which is arguably slightly simpler than the teacher's solution.

However, you should consider that you probably lost marks for reasons other than the teacher mistakenly thinking that you were wrong. I wouldn't allocate five marks just for writing down a simple regular expression. To me, that would be either a one-mark question (one mark for right, none for wrong) or a two-mark question (two marks for right, one for being close and maybe missing a corner case, none for wrong). For five marks, I'd expect an explanation of why the regular expression does what it's supposed to do.

• I agree with this complete analysis. One quality criterion is that explaining the regular expression is simple, or better that a correctness proof is simple. Another criterion could be the star height which is a measure of complexity of the regular expression. Your solution has star height 2 (because you have a star over a subexpression that already has one), when other solutions have star height one. But knowing this can hardly be expected from beginners. Possibly you teacher misread the answer ... can happen. Can you ask what you did wrong? Dec 7 '14 at 14:54
• The second representation in your answer cannot generate 111 string!!!. You need a kleen star there too Sep 9 '18 at 18:01
• Thanks, Rishabh -- you're absolutely right. I've fixed that, as you suggested. Sep 9 '18 at 18:11