Converting a finite state machine into a regular expression in Python 3