title: Getting file extension using pattern matching in python
body: <p>I am trying to find the extension of a file, given its name as a string. I know I can use the function <code>os.path.splitext</code> but it does not work as expected in case my file extension is <code>.tar.gz</code> or <code>.tar.bz2</code> as it gives the extensions as <code>gz</code> and <code>bz2</code> instead of <code>tar.gz</code> and <code>tar.bz2</code> respectively.<br>
  So I decided to find the extension of files myself using pattern matching.</p>
  
  <pre><code>print re.compile(r'^.*[.](?P&lt;ext&gt;tar\.gz|tar\.bz2|\w+)$').match('a.tar.gz')group('ext')
  &gt;&gt;&gt; gz            # I want this to come as 'tar.gz'
  print re.compile(r'^.*[.](?P&lt;ext&gt;tar\.gz|tar\.bz2|\w+)$').match('a.tar.bz2')group('ext')
  &gt;&gt;&gt; bz2           # I want this to come 'tar.bz2'
  </code></pre>
  
  <p>I am using <code>(?P&lt;ext&gt;...)</code> in my pattern matching as I also want to get the extension.  </p>
  
  <p>Please help.</p>