0

I am trying to get a regular expression that: - Has any sequence of 0 and 1. (Binary only) - And Does not contains 00

I know them separate but how I can combine them?

(?:[0-1]+)+

the above for sequence of 0101 of any kind.

Here is screenshot of the part of the question:

Here is the part of the question

any clue reference would be appreciated.

0

3 Answers 3

1

I came to this form:

0?(1+0?)*

Explained:

  1. 0? - can start with 0
  2. 1+ - non-empty sequence of 1s
  3. 0? - followed by at most one 0
  4. (1+0?)* - 2-3 repeated any number of times
Sign up to request clarification or add additional context in comments.

2 Comments

Adam, the code matches simple 00 . Demo: regex101.com/r/2CFroT/5
Those are 3 separate matches: first 0, second 0 (and an empty string).
0

Regular expressions such as 0?(1+0)* will match against any part of the string so it will match the middle part of a string such as 000011000000, it will match the 0110. To check that the whole string matches need to add the start and end of string anchors, giving ^0?(1+0)*$. This will also match an empty string. To match against a non-empty string we could use ^0?(1+0)+$ but this will not match a string with a single 0. So we need to add an alternative (using |) to match the 0, leading to the total expression ^((0?(1+0?)+)|0)$.

These brackets are capturing brackets, they could be changed to non-capturing forms but that would make the expression bigger and, visually, more complex.

Comments

0

You could try something like this:

(10)+|(01)+|1+

Demo: https://regex101.com/r/2CFroT/4

2 Comments

But it also matches 1001 which has 00... And it won't match 1 nor 0.
The thing is the string must NOT match if it contains any sequence of 00. the above match partially. I will update the question above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.