Replace string operations with regex

I think regexes are a better match (hah!) to this kind of problem.

TODO: move the compilation steps to somewhere where they are run only once.