Regular Expression for a binary string that accepts even number of zeros

Asked

Viewed 297 times

3

As stated in the title, follows the DFA that accepts the proposed language:

inserir a descrição da imagem aqui

My question is about the regular expression of that DFA which is as follows:(1*01*01*)*. Conforms to regular expression, it does not mean that any string coming from there starts with binary 1?

1 answer

3


No, the string doesn’t necessarily have to start with 1.

After all, 1* that is to say "zero or more occurrences of 1". The quantifier * admits zero occurrences, which means that the 1 is not mandatory (see some examples).

Just one detail: regex has a quantifier around the parentheses: (...)*, which means that everything between parentheses can also occur zero times. That is, regex also accepts empty strings (or "nothing" - see here how she finds a match).

Although zero is an even number, then "zero occurrences" of digit 0 is also an "even number of zeros" (but if the string is not empty, there must be at least two zeroes).


If you do not want to consider an empty string valid, you can change the regex to (1*01*01*)+, for the quantifier + means "one or more occurrences", meaning that what is in parentheses must occur at least once. Thus, the empty string is no longer considered valid.

  • Thank you very much for the reply, it was clear. But now I noticed something else that left me in doubt. In the case the string containing only a couple of zeros 00 would be accepted? Or obligatory it will have 0101

  • 1

    @Pirategull A pair of zeros accepts yes: https://regex101.com/r/lwuDlv/4/ - please note that all 1 has a * soon after, then all may occur zero or more times.

  • Gosh, how nice the link you sent, I didn’t know will be very helpful thank you. But I still can’t quite understand how these expressions work. (Edit: now I get it, I was considering it as if it were (01)*. Sorry

  • 1

    @Pirategull Regular Expressions is a very complex subject. I suggest starting with here and then here

  • Perfect! Thanks for the suggestions. If I may ask one last question. On the regex101 website, how do I represent sigma? (which can assume both 0 as 1)

  • 1

    @Pirategull If you want sigma character itself (Σ), just put it on. If you mean you can have one 0 or 1, can use [01]: https://regex101.com/r/WuqtcJ/1/

  • eternally grateful

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.