I would keep the regex, just cache it and reuse the same regex instead of compiling it _everytime_ the function is called.