Question

I am newbie to Regular Expression.

In one of my homework problem, I have been ask to write the regular expression of following.

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

Now, I am not getting how should I start to achieve solution. How to find bits divisible by 4?

Was it helpful?

Solution

/^(1[01]*00|0)$/

A one followed by any number of ones and/or zeroes, ending in two zeroes, or just a single zero.

EDIT: If you don't mind leading zeroes, this will also work:

/^(?:[01]*0)?0$/
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top