0

In general, I have a hard time understand the REs. I have been looking for good references but none has made them clear for me so far. If anyone knows good resources, let me know. I would really appreciate it.

I was going over a reading material on RE and there are several examples i havent been able to understand

..(0 or 0(1 or 11)0)*.. no occurrence of 111

I think for there to be a match, the input has to contain either 0 or 010 or 0110 or just none (sine * makes it optional)

The book says the following are matches 0110 101011000010000101 011000011 which i have no objection to because all of them contain at least either one of the three (0 or 010 or 0110 ).

However, the book says the following are false 1110 1110110111 0101011111000011.

The book claims that ..(0 or 0(1 or 11)0)*.. means no occurrence of 111 Can someone explain to me why that is?

3
  • The regex is saying match 0, or match 0 followed one or two 1s, any number of times. This means no occurrence of 111. Commented Mar 10, 2016 at 4:08
  • @TimBiegeleisen so it's either 0 or 010 or 0110 any number of times but it doesnt say anything about what happens elsewhere does it? like wouldnt it be ok to have 0110111 or 101011000011100101 Commented Mar 10, 2016 at 4:12
  • By far the best regular expression resource is regular-expressions.info Commented Mar 10, 2016 at 4:14

1 Answer 1

0

The actual regular expression for matching a binary number which has no more than two 1s in direct sequence is:

^(0|(0(1|11)0))*$

This regex matches any number of zeroes (the first part) or any number of the following two cases: 010 or 0110. This means that the regex will only match a single 1 or pair 11 in the sequence.

You can explore this regex here:

Regex101

7
  • @stratofortress Please mark this answer correct if it solved your problem, thanks. Commented Mar 10, 2016 at 7:02
  • the way i see it, matches will consist of the following: 0 0110 010 Then, this tells me that you are not supposed to end or start with 1. (and you cant have 111 either). Then how comes does the book say 101011000010000101 and 011000011 are matches Commented Mar 10, 2016 at 15:45
  • The reason they are matches is because 0, 010, or 0110 can be repeated any number of times (this is what the * operator means). So they are matches because they consist of fragments which we know will be OK to string together. Does this make sense? Commented Mar 10, 2016 at 15:48
  • what about the two dots on either side of the parentheses? doesnt this mean you have to have two digits on either side of 0 010 and 0110 ? So 01100011 is not a match because it needs two more digits in front ? Based on your comment, it sounds like .. and .. front and back play no role at all so i think im still missing something Commented Mar 10, 2016 at 15:51
  • Actually, you may have found a flaw in the regex, in that it excludes any string which ends in 1 (and 011 is clearly acceptable). Commented Mar 10, 2016 at 15:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.