Added check for invalid regex patterns