-2

As we can develop regular expression like string ending with 01 over {0,1} Like (0+1)*01

So this way over {0,1} I want the answer of my question that what will be the regular expression for number of 0s and 1s both are odd

Any combination of 1 and 0 should work.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • 1
    You did not put sensible tags on your question that would help people with the appropriate knowledge find your question. I have done that for you; however your question, perhaps, is not really about programming, but is a computer science class exercise. Perhaps you would be better in [cs.stackexchange.com](http://cs.stackexchange.com/). You should also search through other similar questions. This is such a common problem that I suspect your answer is already there. – Brian Tompsett - 汤莱恩 Jun 01 '15 at 17:23
  • Are you looking for an odd length? – cwallenpoole Jun 02 '15 at 18:18

2 Answers2

1

First solve this problem with a finite state machine, then convert the FSM to a regular expression (this is always possible). You can construct a FSM with four states: both even, both odd, 1s even and 0s odd, and 0s even and 1s odd. Start in state "both even" and move in the obvious way; "both odd" is the only accepting state.

For the conversion, see https://cs.stackexchange.com/q/2016/1531.

Community
  • 1
  • 1
Charles
  • 11,269
  • 13
  • 67
  • 105
  • I know that from FSM we can generate regular expression.but in this case it is not working with me that is why i have asked. It is possible to draw it's FSM but not possible to create a regular expression according to me –  Jun 04 '15 at 08:35
0

An general "odd" count for "x" is (([^x]*x){2})*[^x]*x[^x]*. Use two look aheads with this kind of expression to assert that both characters 1 and 0 are present an odd number of times:

^(?=(0*10*1)*0*10*$)(?=(1*01*0)*1*01*$).*

See live demo.

The regex is basically 2 look aheads, which have the form (?=...) (where ... is an expression) and assert (without consuming) that the input following matches the expression. Because it doesn't consume the input, it doesn't move the pointer, so you can have multiple look aheads at the same point.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • Your expression is not working.. It is generating so many wrong strings which should not be in language –  Jun 04 '15 at 08:38
  • @himavyas please give examples of incorrectly matched input. – Bohemian Jun 04 '15 at 09:19
  • i can generate 111 from 1st half.. And from the second half also so string 111111 is not valid still generated by this expression –  Jun 04 '15 at 11:15
  • I got one.. 111 can not be accepted because here number of 1s are odd but number of 0s is even(because no occurrence is said to be even). –  Jun 04 '15 at 11:24
  • @himavyas but `111111` is not matched by my regex - [see demo](http://rubular.com/r/NRNIeKf7Pi). Did you copy paste my regex exactly? Which regex engine/language are you using? – Bohemian Jun 04 '15 at 17:44
  • Yes it was my mistake 111111 is not matched but what about 111 it is accepted while it is not in the language. Because in 111 number of 0s are even.(no occurrence of 0 will be considered as even) it is accepted by your regex. –  Jun 05 '15 at 09:02
  • your regex is correct. Thank you so much and sorry for inconvenience –  Jun 05 '15 at 09:07
  • Will you explain what is the meaning of ?= in terms of regex? –  Jun 05 '15 at 09:16
  • @hima short explanation added. Google "regex look ahead" (and "regex look behind") – Bohemian Jun 05 '15 at 18:12