title: Non-greedy string regular expression matching
body: <p>I'm pretty sure I'm missing something obvious here, but I cannot make R to use non-greedy regular expressions:</p>
  
  <pre><code>&gt; library(stringr)
  &gt; str_match('xxx aaaab yyy', "a.*?b")                                         
       [,1]   
  [1,] "aaaab"
  </code></pre>
  
  <p>Base functions behave the same way:</p>
  
  <pre><code>&gt; regexpr('a.*?b', 'xxx aaaab yyy')
  [1] 5
  attr(,"match.length")
  [1] 5
  attr(,"useBytes")
  [1] TRUE
  </code></pre>
  
  <p>I would expect the match to be just <code>ab</code> as per 'greedy' comment in <a href="http://stat.ethz.ch/R-manual/R-devel/library/base/html/regex.html" rel="noreferrer">http://stat.ethz.ch/R-manual/R-devel/library/base/html/regex.html</a>:</p>
  
  <blockquote>
    <p>By default repetition is greedy, so the maximal possible number of repeats is used. 
        This can be changed to ‘minimal’ by appending ? to the quantifier. (There are further 
        quantifiers that allow approximate matching: see the TRE documentation.)</p>
  </blockquote>
  
  <p>Could someone please explain me what's going on?</p>
  
  <p><strong>Update.</strong> What's crazy is that in some other cases non-greedy patterns behave as expected:</p>
  
  <pre><code>&gt; str_match('xxx &lt;a href="abc"&gt;link&lt;/a&gt; yyy &lt;h1&gt;Header&lt;/h1&gt;', '&lt;a.*&gt;')
       [,1]                                          
  [1,] "&lt;a href=\"abc\"&gt;link&lt;/a&gt; yyy &lt;h1&gt;Header&lt;/h1&gt;"
  &gt; str_match('xxx &lt;a href="abc"&gt;link&lt;/a&gt; yyy &lt;h1&gt;Header&lt;/h1&gt;', '&lt;a.*?&gt;')
       [,1]              
  [1,] "&lt;a href=\"abc\"&gt;"
  </code></pre>