You can use an automaton to work for you