title: Return first instance of capturing group if found, otherwise empty string
body: <p>My inputs are strings that may or may not contain a pattern:</p>
  
  <pre><code>p = '(\d)'
  s = 'abcd3f'
  </code></pre>
  
  <p>I want to return the capturing group for the first match of this pattern if it is found, and an empty string otherwise.</p>
  
  <pre><code>result = re.search(p, s)[1]
  </code></pre>
  
  <p>Will return the first match. But if <code>s = 'abcdef'</code> then <code>search</code> will return <code>None</code> and the indexing will throw an exception. Instead of doing that, I'd like it to just return an empty string. I can do:</p>
  
  <pre><code>g = re.search(p, s)
  
  result = ''
  if len(g) &gt; 0: result = g[1]
  </code></pre>
  
  <p>Or even:</p>
  
  <pre><code>try:
      result = re.search(p, s)[1]
  except:
      result = ''
  </code></pre>
  
  <p>But these both seem pretty complicated for something so simple. Is there a more elegant way of accomplishing what I want, preferably in one line?</p>