title: Get the spans of named groups in order in one line
body: <p>I have a regex like this <code>r'(?P&lt;mark1&gt;)|\s+(?P&lt;mark2&gt;)|(?P&lt;mark3&gt;)\s+'</code> (sample regex not the actual one) and I want to get the spans of the captured groups in matches in order.</p>
  
  <p>For example:<br>
  1. <code>[match.span() for match in re.finditer(regex, string)]</code> returns the spans in order but gives the span of whole match not just the captured group.<br>
  2. <code>[match.span('mark1') for match in re.finditer(regex, string)]</code> returns the spans in order of captured groups but puts <code>(-1, -1)</code> for other named groups.</p>
  
  <p>So, can I get the spans of the named groups in order of the matches in one line, as simple as the above queries?</p>
  
  <p>I found the following way:<br>
  <code>[match.span(name) for match in re.finditer(regex, string) for name, value in match.groupdict().items() if value is not None]</code></p>
  
  <p>Is there a simple one?</p>
  
  <p>An example to present my scenario:</p>
  
  <pre><code>import re
  s = "asfasdf      32392  ..///?%        aslf    /././/               342"
  reg = r'(?P&lt;mark1&gt;[a-z]+)|\s+(?P&lt;mark2&gt;[0-9]+)|(?P&lt;mark3&gt;[./?%]+)\s+'
  print([match.span(name) for match in re.finditer(reg, s) for name, value in match.groupdict().items() if value is not None])
  print([match.span() for match in re.finditer(reg, s)])
  print
  print([match.span('mark1') for match in re.finditer(reg, s)])
  print([match.span('mark2') for match in re.finditer(reg, s)])
  print([match.span('mark3') for match in re.finditer(reg, s)])
  </code></pre>
  
  <p>Output:  </p>
  
  <pre><code>[(0, 7), (13, 18), (20, 27), (35, 39), (43, 49)]
  [(0, 7), (7, 18), (20, 35), (35, 39), (43, 64)]
  
  [(0, 7), (-1, -1), (-1, -1), (35, 39), (-1, -1)]
  [(-1, -1), (13, 18), (-1, -1), (-1, -1), (-1, -1)]
  [(-1, -1), (-1, -1), (20, 27), (-1, -1), (43, 49)]
  </code></pre>