title: How to check if a string is a valid regex in Python?
body: <p>In Java, I could use the following function to check if a string is a valid regex (<a href="https://stackoverflow.com/questions/6341367/how-to-check-if-the-string-is-a-regular-expression-or-not">source</a>):</p>
  
  <pre><code>boolean isRegex;
  try {
    Pattern.compile(input);
    isRegex = true;
  } catch (PatternSyntaxException e) {
    isRegex = false;
  }
  </code></pre>
  
  <p>Is there a Python equivalent of the <code>Pattern.compile()</code> and <code>PatternSyntaxException</code>? If so, what is it?</p>