Major rewrite using reular expressions. This requires the availability of a regex engine (mruby-pcre or mruby-oniguruma)