improve regex, implement pattern cache