0

I was trying to make regular expression for the strings that accept even number of 0's and even number of 1's. But I was not able to do so. Please any one can help?

I tried making

(0000+0011+0101+0110+1111+1100+1010+1001)*(^+00+11)

but it does not accept the string 100001.

I read that question that @vks has addressed but was not able to understand the answers properly. I tried to add a comment but my rep is below 50, so am not able to do that, that's why I asked a new question.

serenesat
  • 4,611
  • 10
  • 37
  • 53
slvi
  • 73
  • 1
  • 8

2 Answers2

3

Here is a simple regular expression which will match an even number of zeroes or ones:

^(00|11|(01|10)(00|11)*(01|10))*$

This was tested on the site Regular Expressions 101.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • There is serious problem on this site with newer users not making any effort to search the already existing SO database, q.v. [this answer here](http://stackoverflow.com/questions/2725522/regular-expression-for-strings-with-an-even-number-of-zeroes-and-ones) to essentially the same question. – Tim Biegeleisen Mar 12 '15 at 05:19
  • Thanx...have marked this duplicate!!!!!!!! – vks Mar 12 '15 at 05:21
  • i have edited my question.and one more problem is there,that we new users cannot comment on the existing questions if we have a confusion. – slvi Mar 13 '15 at 07:05
  • and i am not able to understand this regex.can it be made using only + and *? – slvi Mar 13 '15 at 07:09
  • You would not really want to use the + operator in my solution, because that would mean allowing something to occur 0 **or** 1 times. We are trying to match only an even number of 0s or 1s, hence the pairs of 0s and 1s which you see in the regex. – Tim Biegeleisen Mar 13 '15 at 07:11
  • actually the problem is that i am not able to understand what this ^,| and $ sign holds for.I dont know their functioning. – slvi Apr 07 '15 at 09:09
  • 2
    `^` means the **start** of the string and `$` means the end of the string. – Tim Biegeleisen Apr 07 '15 at 09:12
1
^(?![^0\n]*0[^0\n]*(?:0[^0\n]*0)*[^0\n]*$)(?![^1\n]*1[^1\n]*(?:1[^1\n]*1)*[^1\n]*$)[10]*$

This will match only string with even no of 0's and 1's.This uses lookahead so for long string it will take time.See demo.

https://regex101.com/r/iS6jF6/11

vks
  • 67,027
  • 10
  • 91
  • 124
  • i dont understand these symbols like ? ^ etc. I use only + and * in my regular expression . i dont know about any other symbol please explian other symbol – slvi Mar 12 '15 at 05:13
  • 1
    @slvi check the link.On the right sidde you can see all the explanations – vks Mar 12 '15 at 05:14
  • i am still not able to understand.i dont know anything about these lookaheads etc.i make simple regex and i dont know anything about using new line etc – slvi Mar 13 '15 at 07:07
  • @slvi you can simpy use `^(00|11|(01|10)(00|11)*(01|10))*$` without lookaheads – vks Mar 13 '15 at 07:11
  • no.I am not able to understand thes symbols ^,| and $.can you give an explaination what these symbols do? – slvi Apr 07 '15 at 09:11
  • 2
    `^`===starting of string,`|` is alternation operator ie `A|B` mean either `A` or `B`.`$` is end of string. – vks Apr 07 '15 at 09:12
  • Asking the same question twice is a good way to get an answer ^ ^ – Tim Biegeleisen Apr 07 '15 at 09:13
  • @TimBiegeleisen we both r trolled :P – vks Apr 07 '15 at 09:14
  • thankew.now i got it.actually i use + instead of | that was why i was not able to understand.now i got it :) – slvi Apr 07 '15 at 09:27