Regular expression to match strings 0 and 1 without substituting "011"

I am working on a problem (from "An Introduction to the Theory of Automata, Languages, and Computers" by Hopcroft, Motwani, and Ullman) to write a regular expression that defines a language consisting of all lines 0 and 1 that do not contain substring 011 .

Is the answer (0+1)* - 011 ? If not what should be the right answer to this?

+4
source share
2 answers

Automata diagram Edit: Updated to include startup states and fixes as follows.

+9
source

If you are looking for all strings that do not have 011 as a substring, and not just exclude string 011 :

A classic regex for this would be:

 1*(0+01)* 

Basically, you can have as many at the beginning as you want, but as soon as you hit zero, it's either zeros or zero that follow (since otherwise you will get zero-one-one).

A modern, not very regular regular expression will be:

 ^((?!011)[01])*$ 

IF, however, you want some string not to be 011 , you can just list the short string and the wildcard:

 λ+0+1+00+01+10+11+(1+00+010)(0+1)* 

And in modern regular expression:

 ^(?!011)[01]*$ 
+3
source

Source: https://habr.com/ru/post/1307218/


All Articles