a more robust check on the outcome of `regexec` and a more clean design