title: In Python Regular Expressions, how can I disregard an expression inside a captured group?
body: <p>I have a string that looks like this:</p>
  
  <pre><code>&lt;name&gt;-&lt;gender&gt;-&lt;age&gt;.jpg
  </code></pre>
  
  <p>I want to be very liberal as far as what I accept. Requirements being:</p>
  
  <ol>
  <li>The <code>&lt;name&gt;</code> component is required.</li>
  <li>Must have the <code>.jpg</code> file extension</li>
  <li>You may leave a component blank or leave it out entirely as long as the end result is one of the following permutations:
  <ul>
  <li><code>&lt;name&gt;.jpg</code></li>
  <li><code>&lt;name&gt;-&lt;gender&gt;.jpg</code></li>
  <li><code>&lt;name&gt;-&lt;gender&gt;-&lt;age&gt;.jpg</code></li>
  </ul></li>
  </ol>
  
  <p>Examples of what is considered valid:</p>
  
  <pre><code>Beamin-M.jpg
  Jean.jpg
  Maria-F-23.jpg
  </code></pre>
  
  <p>I want to break down the each component of the string using regular expressions but I do not want to capture the dash (<code>-</code>). I tried using non-capturing groups but was not able to get the results I was looking for:</p>
  
  <pre><code>&gt;&gt;&gt; import re
  &gt;&gt;&gt; r = re.compile(r'([^\-]*)((?:\-)[^\-]*)?((?:\-)[^\-]*)?\.jpg')
  &gt;&gt;&gt; for d in (
  ...  'Beamin-M.jpg',
  ...  'Jean.jpg',
  ...  'Maria-F-23.jpg',
  ... ):
  ...  print r.match(d).groups()
  ...
  ('Beamin', '-M', None)
  ('Jean', None, None)
  ('Maria', '-F', '-23')
  </code></pre>
  
  <p>Does anyone have any suggestions?</p>