Hi. Good question! Well, just like what we have in Java, for Python we have re.error exception just for this very purpose. Check out the code below to understand it better:
is_valid = True
is_valid = False
What is re.error exception, you ask?
Here is the formal definition I got from the official documentation:
"Exception raised when a string passed to one of the functions here is not a valid regular expression or when some other error occurs during compilation or matching. "
Hope this helped!