I am self-studying regular expressions and found an interesting practice problem online that involves writing a regular expression to recognize all binary numbers divisible by 3
Binary numbers divisible by 3 fall into 3 categories:
(ex. 11, 110, 1100,1001,10010, 1111)
(decimal: 3, 6, 12, 9, 18, 15)
(ex. 10101, 101010, 1010001, 1000101)
(decimal: 21, 42, 81, 69)
(ex. 1010111, 1110101, 1011100110001)
(decimal: 87, 117, 5937)
So a regular expression that takes into account these three rules is simply:
0*(1(00)*10*|10(00)*1(00)*(11)*0(00)*10*)*0*
How to read it:
() encapsulate
* means the previous number/group is optional
| indicates a choice of options on either side within the parentheses