Modified to cache compiled regular expression into dict in order to reduce computation