replace regular expression with tokenizer

This change lays the groundwork for the addition of Python's format
specification mini-language. Incidentally, it fixes the handling of
unmatched, unescaped curly brackets.