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...exceptandre.compilein python? will there.compilevalidate regex?re.compile("(" * 1000 + "a" + ")"*1000)will fail withRuntimeError: maximum recursion depth exceededinstead ofre.error