Added error handling of regex matching