title: Python - regex search for string which starts and ends with the given text
body: <p>I have a list of files, and I want to keep only the ones which start with 'test_' and end with '.py'.  I want the regex to only return the text inside the 'test_' and '.py'.  I do not want .pyc files included.</p>
  
  <p>I have tried:</p>
  
  <pre><code>&gt;&gt;&gt;filename = 'test_foo.py'
  &gt;&gt;&gt;re.search(r'(?&lt;=test_).+(?=\.py)', filename).group()
  foo.py
  </code></pre>
  
  <p>but it still returns the extension, and will allow '.pyc' extensions (which I do not want).  I'm pretty sure it's the '+' which is consuming the whole string.</p>
  
  <p>This works as a fallback, but I would prefer a regex solution:</p>
  
  <pre><code>&gt;&gt;&gt;filename = 'test_foo.py'
  &gt;&gt;&gt;result = filename.startswith('test_') and filename.endswith('.py')
  &gt;&gt;&gt;result = result.replace('test_', '').replace('.py', '')
  &gt;&gt;&gt;print result
  foo
  </code></pre>