title: Regex for optional end-part of substring
body: <p>Consider the following (highly simplified) string:</p>
  
  <pre><code>'a b a b c a b c a b c'
  </code></pre>
  
  <p>This is a repeating pattern of <code>'a b c'</code> except at the beginning where the <code>'c'</code> is missing.</p>
  
  <p>I seek a regular expression which can give me the following matches by the use of  <code>re.findall()</code>:</p>
  
  <pre><code>[('a', 'b'), ('a', 'b', 'c'), ('a', 'b', 'c'), ('a', 'b', 'c')]
  </code></pre>
  
  <p>The string above thus have 4 matches of <code>'a b c'</code> - although with the first match as a special case since the <code>'c'</code> is missing.</p>
  
  <p>My simplest attempt is where I try to capture <code>'a'</code> and <code>'b'</code> and use an optional capture for <code>'c'</code>:</p>
  
  <pre><code>re.findall(r'(a).*?(b).*?(c)?', 'a b a b c a b c a b c')
  </code></pre>
  
  <p>I get:</p>
  
  <pre><code>[('a', 'b', ''), ('a', 'b', ''), ('a', 'b', ''), ('a', 'b', '')]
  </code></pre>
  
  <p>Clearly, it has just ignored the <code>c</code>. When using non-optional capture for <code>'c'</code> the search skips ahead prematurely and misses <code>'a'</code> and <code>'b'</code> in the second <code>'a b c'</code>-substring. This results in 3 wrong matches:</p>
  
  <pre><code>[('a', 'b', 'c'), ('a', 'b', 'c'), ('a', 'b', 'c')]
  </code></pre>
  
  <p>I have tried several other techniques (for instance, <code>'(?&lt;=c)'</code>) to no avail.</p>
  
  <p>Note: The string above is just a skeleton example of my "real-world" problem where the three letters above are themselves strings (from a long log-file) in between other strings and newlines from which I need to extract named groups.</p>
  
  <p>I use Python 3.5.2 on Windows 7.</p>