cloud
cloud

Reputation: 11

Regex with exactly one block of 0s with even length

The alphabet is {0, 1}.

A "block of 0s" means a substring of 0s not contained in a longer substring of 0s.

I came up with: (0(00)*)* 1* 00(00)* 1* (0(00)*)*

Is it correct?

Upvotes: 0

Views: 198

Answers (3)

JJoao
JJoao

Reputation: 5347

grep -oP '(?<=^|1)(00)+(?=1|$)' input
  • (00)+ -- even number of zeros
  • (?<= ^ | 1 ) -- before (left context): a "1" or the start of the string
  • (?= 1 | $ ) -- after (right context): a "1" or the end of the string
  • grep -oP -- regexp can be used in many different ways; -P -- to include Perl extensions ; grep -o -- print only the matched parts

with input

000
0000
00001
11100110000001

outputs

0000
0000
00
000000

Upvotes: 1

Bohemian
Bohemian

Reputation: 425003

For exactly one block of zeros of even length with a vocab of 1 and 0:

^1*(00)+1*$

Upvotes: 1

deme72
deme72

Reputation: 1153

this should work

 (\b(00){1,})(?!0)

also I tried your solution and it didn't work

Upvotes: 0

Related Questions