2
$\begingroup$

I'm trying to construct a regular expression for the language of binary strings in which every 00 must have at least two 1s before it.

I realize this can be done with lookbehinds using the following regex: ^(?:(?<=11)00(?!0)|(?<=1)0|1+)+$ , however I am now trying to construct a regular expression in the sense of formal language theory.

I currently have (0|)(11*0)*1* (or (0+ε)(11*0)*1* ), which gives me the language of binary strings without consecutive 0s, but am not quite sure how to get the 'conditional' part in without actually using a conditional.

Accepted:
0
1
10
11100
1100

Rejected:
100
1000
11000

$\endgroup$
4
  • $\begingroup$ What is the "conditional", in your opinion? It's easy enough to do alternation, and nothing requires alternation to be exclusive: $(0|)(111^*00|11^*0)^*1^*$ $\endgroup$ – rici Jan 26 at 22:35
  • $\begingroup$ by 'conditional' i was referring to the 'if there is 00, there must at least 2 1s' condition. i am still trying to wrap my head around this all, i am curious what type of approach you used to come up with that expression, is there a technique I can use to derive a formal regex from a language? $\endgroup$ – Rolodecks Jan 26 at 23:35
  • 2
    $\begingroup$ Regular expressions don't have conditionals so you need to come up with a different way of thinking about them. As I said, alternation works so one approach is to enumerate possibilities, which is what I did: 00 must follow two or more 1s; a single 0 must follow one or more 1s (otherwise it wouldn't be a single 0). $\endgroup$ – rici Jan 27 at 0:23
  • 1
    $\begingroup$ ^(?:(?<=11)00(?!0)|(?<=1)0|1+)+$ does not match those strings that start with 0, such as 0, or 01, or 010, etc. It also misses the empty string. $\endgroup$ – John L. Jan 28 at 2:10
1
$\begingroup$

Here is a way to describe a binary string in which every 00 must have at least two 1s before it.

It is a string that
$\quad$ starts with zero or one 0;
$\quad$ followed by zero or more blocks, where each block is
$\quad\quad$ either one or more 1s followed by a 0, such as 10, 110, 1110, etc,
$\quad\quad$ or two or more 1s followed by two 0s, such as 1100, 11100, 111100, etc;
$\quad$ followed by zero or more 1s.

Translated into regular expression, the description above becomes
$\quad$ (0|ε)(11*0|111*00)*1*


You might want to ask, how can we arrive that kind of description?

The observation here is that the number of consecutive 0s in the string is at most 2, a very small number. That indicates that we can break down the string by enumerate different possibilities of consecutive 0s. To paraphrase rici's comment, we could try using enumeration to simulate conditionals.


Of course, there is a mild stroke of luck involved in the approach above on that language. As a last resort, we can always use a heavier machinery such as constructing a DFA for the language (which can be done in one or several ways) and then converting the DFA to a regular expression.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.