What can be regular expression for this statement | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 10

What can be regular expression for this statement

All string of 0's and 1's in which the total number of 0's to the right of each 1 is even [Edit] Thanks for all your answer and help this might be the final regular expression of I am right but didn't checked it yet though https://www.sololearn.com/post/156151/?ref=app

4th Oct 2019, 5:41 AM
Preity
Preity - avatar
19 Answers
+ 11
Supplying examples of valid/invalid strings would have made it a lot easier to understand the required rules: Do zeroes must have a leading 1's? If the string can begin with zeroes, can their amount be odd since they don't have a leading 1? here's a code that covers 3 variations (description in code + comments) https://code.sololearn.com/WQAvh32qLvKT/?ref=app
4th Oct 2019, 8:26 AM
Burey
Burey - avatar
+ 7
maybe it helps 🤷‍♂️😅 https://code.sololearn.com/cTeN6rAn7f22/?ref=app
4th Oct 2019, 6:31 AM
Anton Böhler
Anton Böhler - avatar
+ 7
~ swim ~ I feel with you regex is 😭😭😭to me.
4th Oct 2019, 10:21 AM
Oma Falk
Oma Falk - avatar
+ 5
~ swim ~ you are an genius in other area of programming though 👍
4th Oct 2019, 5:56 AM
Preity
Preity - avatar
+ 5
Oma Falk i came up with 3 😅
4th Oct 2019, 10:57 AM
Burey
Burey - avatar
+ 4
what's your string ? what i can break down from your current question is you need an even number of 0. what is even ? i would say as long as its dividable by 2 its even. so (00) come to mind. it simply means twozeroes. but maybe you'll need 2 zeroes a few times. we can use + which is means one or more occurrence. now the regex is (00)+ now we need to make sure 1 is appear next. (00)+1 done. if you need 1 appear once or more you can use 1+ instead of 1.
4th Oct 2019, 6:38 AM
Taste
Taste - avatar
+ 4
Thanks Burey for all test casses answer. And thanks all other too Taste , ~ swim ~ Anton Böhler thanks for an another approach 👍
4th Oct 2019, 9:21 AM
Preity
Preity - avatar
+ 4
Preity I made it a coding challenge. Your prob is interesting and not too hard to code. Anton Böhler will agree Burey maybe too I am sure, someone will come around with a regex. Also this is a little tip: Next time make it a coding challenge yourself 😁😁😁
4th Oct 2019, 10:36 AM
Oma Falk
Oma Falk - avatar
+ 3
Lol you literally know all the answers ~ swim ~ 😎
4th Oct 2019, 6:15 AM
Onimisi
Onimisi - avatar
+ 3
Burey better is it "Wer vieles bringt, wird manchem etwas bringen" Goethe, Faust Vorspiel auf dem Theater
4th Oct 2019, 11:31 AM
Oma Falk
Oma Falk - avatar
+ 3
Oma Falk thank you so much about saying that coding challenge but I didn't make that one an coding challenge as I've making an code about designing the automata both deterministic and non deterministic from which whenever I get back it's easy to understand. But I stuck to make any regular expression from that automata the code from Burey quite clear some cases so I'm trying with those. Let's see if some other regex can be created as you make that as post I would make all of automata by that and post in an code or post later. thanks for making that post as an challenge btw I didn't know about the challenge thing 👍 👍
4th Oct 2019, 12:01 PM
Preity
Preity - avatar
+ 3
I would add that whenever an number in string occurs as 1 then it's right side will have even number of 0's for example:- 10010000100 -> true 1010010000 -> false And all the cases some of which has solved by burey with nice regex. Thank you all for your time and help
4th Oct 2019, 12:10 PM
Preity
Preity - avatar
+ 3
Anton Böhler is this explain all things which I tried to make and show after seeing here so many expressions I got to make this one as final not know it's perfect or not https://www.sololearn.com/post/156151/?ref=app
4th Oct 2019, 12:29 PM
Preity
Preity - avatar
+ 3
My bad, sorry.
4th Oct 2019, 10:14 PM
Divine Darkey
Divine Darkey - avatar
+ 2
Preity what of these are valid? (it would minimize confusion alot probably) 1.) 100 2.) 1 3.) 1100 4.) 00 5.) 000 6.) 00100 7.) 001 8.) 0001
4th Oct 2019, 12:14 PM
Anton Böhler
Anton Böhler - avatar
+ 2
~ swim ~ welcome. But I would say that you deserve all thanks as you have solved many of my difficulties whenever me facing thanks a lot for that And thanks to everyone who helped me till now. I think this section and code section is the best as compare to the challenge and other things 👍 👍
4th Oct 2019, 6:31 PM
Preity
Preity - avatar
+ 1
~ swim ~ When an 0 apper at right side in an automata then it must be in even else the 1 without 0 on right side is acceptable by the automata. I'm just designing the regex for automata working which later can help me to make Turing machine pattern matching easy with the regex. Leading 0's to can appear when you make state diagram so I'm considering both the cases for evaluation
4th Oct 2019, 6:24 PM
Preity
Preity - avatar
+ 1
regex for form validation
5th Oct 2019, 5:36 PM
Mostafa
Mostafa - avatar
0
edited: import re def check_for_even_zeros(mystr): patten = '10+' for x in re.findall(patten, mystr): if x.count('0') % 2 == 1: return False return True mystring1 = '100100100100' # the one has all even zeros. print(check_for_even_zeros(mystring1)) # so this prints true. mystring2 = '1001001001000' # this one has odd number of zeros at the end. print(check_for_even_zeros(mystring2)) # so this prints false
5th Oct 2019, 9:15 PM
rodwynnejones
rodwynnejones - avatar