title: Determine Python regex named group numbers?
body: <p>In Python you can have a regex with named groups, such as:</p>
  
  <pre><code>m = re.match(r"(?P&lt;first_name&gt;\w+) (?P&lt;last_name&gt;\w+)", "Malcolm Reynolds")
  </code></pre>
  
  <p>You can look-up the matched text for the named groups by name or using <code>groupdict</code>:</p>
  
  <pre><code>In [202]: m.group("first_name")
  Out[202]: 'Malcolm'
  
  In [203]: m.groupdict()
  Out[203]: {'first_name': 'Malcolm', 'last_name': 'Reynolds'}
  </code></pre>
  
  <p>If I want offsets for the named groups, I need to know which group number they are, so I can use something like <code>start(1)</code>.  How can I find out the mapping between the named groups and their group numbers?  If they matched the same strings, this could not be unambiguously inferred.</p>
  
  <p>The purpose of this to be able to infer the named annotated spans (with offsets) in a function which has no specific knowledge of the original regex. </p>