Thursday, March 28, 2013

Regular Expression to Match Multiples of 3

Alok Menghrajani (via Nicolas Seriot):

An easier way to solve the problem requires understanding that regular expressions can be written as finite state machines, and vice versa. […] The state machine is going to process the input and keep track of the sum of the digits. We only need the sum modulo 3, so we’ll have a pretty simple state machine with 3 states: state A (starting state), state B (we are off by 1), state C (we are off by 2).

Comments RSS · Twitter

Leave a Comment