Search code examples
regexregular-language

Regular expression for no more than 1 zero in a row


I need to write regular expression for language {0,1} with no more than one zero in a row.


Solution

  • The regex expression would be:-

    0?(1+0)*1*

    Since, there can be a single zero at the start, so we have 0?, meaning it either present singly or absent.

    The next (1+0) means, we see a series of (at least one) 1's followed by a single zero. This whole group of a zero following a series of 1's can be repeated multiple times. And, we also add 1*, at the end to avoid our pattern to always detect the required strings but always ending in zero.