Implement detect using regexes

The code used char-by-char iteration before, but it was inelegant, and
regexes have comparable performance. For super-long strings, we can
always just tokenize to avoid O(n).

(github: fixes #1 and fixes #4)