In Java, I could use the following function to check if a string is a valid regex (source):
boolean isRegex;
try {
Pattern.compile(input);
isRegex = true;
} catch (PatternSyntaxException e) {
isRegex = false;
}
Is there a Python equivalent of the Pattern.compile()
and PatternSyntaxException
? If so, what is it?
try...except
andre.compile
in python? will there.compile
validate regex? – alvasre.compile("(" * 1000 + "a" + ")"*1000)
will fail withRuntimeError: maximum recursion depth exceeded
instead ofre.error
– dbr