shubham
shubham

Reputation: 43

Regular expression for binary numbers which are divisible by 4

I want to create a regular expression to match the following:

L: the set of all bit strings (i.e. strings over alphabet {0,1}) that are divisible by 4

Upvotes: 0

Views: 3934

Answers (1)

Poul Bak
Poul Bak

Reputation: 10929

If a binary is divisible by four, the last two bits are zero. So you can use this Regex to match:

/.+00$/

or, if you want to check that it is indeed a binary number (only zeros and ones), you can use:

/[01]+00$/

If you also want to match 0 and 00:

/^(00?|[01]+00)$/

if you don't want to match all zeros, you can use:

/(?=1)[01]+00$/

Upvotes: 5

Related Questions