re.sub: non-greedy doesn't work as expected
<p>I have the following code in ipython. I expect it to remove the beginning "ab" since .*? is a non-greedy one. But why it remove all the way up to the last b?</p>

<pre><code>  In [15]: b="abcabcabc"

  In [16]: re.sub(".*?b","",b)
  Out[16]: 'c'
</code></pre>

<p>The <em>python</em> <a href="https://docs.python.org/3/library/re.html#re.sub" rel="nofollow">docs</a> says:</p>

<blockquote>
  <p>The optional argument count is the maximum number of pattern occurrences to be replaced; count must be a non-negative integer. If omitted or zero, <strong>all occurrences will be replaced</strong></p>
</blockquote>

<p>So, you can call <code>re.sub</code> with <code>count=1</code> to get your desired result:</p>

<pre><code>re.sub(".*?b", "", b, 1)
#output
'cabcabc'
</code></pre>

python regex