bingjie2680
bingjie2680

Reputation: 7773

Match a binary string which contains a sub set that should not be all zeros

As the title states, I am trying to make a regular expression that, given arbitrary positions, matches a binary string which contains a sub set that should not be all zeros( or at least one 1), for example:

binary:   100101
          -------
position: 123456

when arbitrary position is 123, then matching return true, because the first one is 1;
when arbitrary position is 235, then matching return false, because none of them is 1;
when arbitrary position is 236, then matching return true, because the last one is 1;

I know some regular expression, but I really don't know how to approach this one. any help would be highly appreciated.

Upvotes: 0

Views: 134

Answers (1)

Joe
Joe

Reputation: 31087

Assuming your string can consist only of 1s and 0s, invert the test. Construct a regex that matches zero in each arbitrary position and then test that it doesn't match.

For each position, store a '0' at that index. Otherwise, a '.' to allow any character.

For example:

235 -> .00.0.

Then test for no match:

.00.0. matches '100101'

so 235 would be false.

Upvotes: 4

Related Questions