title: Python Conditional Regular Expression
body: <p>This is a question involving a conditional regular expression in python:</p>
  
  <p>I'd like to match the string <code>"abc"</code> with </p>
  
  <pre><code>match(1)="a"
  match(2)="b"
  match(3)="c"
  </code></pre>
  
  <p>but also match the string <code>"  a"</code> with </p>
  
  <pre><code>match(1)="a"
  match(2)=""
  match(3)=""
  </code></pre>
  
  <p>The following code ALMOST does this, the problem is that in the first case <code>match(1)="a"</code>
  but in the second case, <code>match(4)="a"</code> (not <code>match(1)</code> as desired).  </p>
  
  <p>In fact, if you iterate through all the groups with <code>for g in re.search(myre,teststring2).groups():</code>, you get 6 groups (not 3 as was expected).</p>
  
  <pre><code>import re
  import sys
  
  teststring1 = "abc"
  teststring2 = "  a"
  
  myre = '^(?=(\w)(\w)(\w))|(?=\s{2}(\w)()())'
  
  if re.search(myre,teststring1):
      print re.search(myre,teststring1).group(1)
  
  if re.search(myre,teststring2):
     print re.search(myre,teststring2).group(1)
  </code></pre>
  
  <p>Any thoughts? (note this is for Python 2.5)</p>