title: Non-greedy matching with grep
body: <p>Non greedy matching as far as I know is not part of Basic Regular Expression (BRE) and Extended Regular Expression (ERE). However, the behaviour on different versions of <code>grep</code> (BSD and GNU) seems to suggest other wise. </p>
  
  <p>For example, let's take the following example. I have a string say: </p>
  
  <pre><code>string="hello_my_dear_polo"
  </code></pre>
  
  <h2>Using GNU <code>grep</code>:</h2>
  
  <p>Following are few attempts to extract <code>hello</code> from the string. </p>
  
  <p><strong>BRE Attempt</strong> (fails)<strong>:</strong></p>
  
  <pre><code>$ grep -o "hel.*\?o" &lt;&lt;&lt; "$string"
  hello_my_dear_polo
  </code></pre>
  
  <p>Output yields entire string which suggest the non-greedy quantifier does not work on BRE. Note that I have only escaped <code>?</code> since <code>*</code> does not lose it's meaning and need not be escaped. </p>
  
  <p><strong>ERE Attempt</strong> (fails)<strong>:</strong></p>
  
  <pre><code>$ grep -oE "hel.*?o" &lt;&lt;&lt; "$string"
  hello_my_dear_polo
  </code></pre>
  
  <p>Enabling the <code>-E</code> option also yields the same output suggesting that non-greedy matching is not part of ERE. Escaping was not needed here since we are using ERE.</p>
  
  <p><strong>PCRE Attempt</strong> (succeeds)<strong>:</strong></p>
  
  <pre><code>$ grep -oP "hel.*?o" &lt;&lt;&lt; "$string"
  hello
  </code></pre>
  
  <p>Enabling the <code>-P</code> option for PCRE suggests that non-greedy quantifier is a part of it and hence we get the desired output of <code>hello</code>. Escaping was not needed here since we are using PCRE.</p>
  
  <h2>Using BSD <code>grep</code>:</h2>
  
  <p>Here are few attempts to extract <code>hello</code> from the string. </p>
  
  <p><strong>BRE Attempt</strong> (fails)<strong>:</strong></p>
  
  <pre><code>$ grep -o "hel.*\?o" &lt;&lt;&lt; "$string"
  </code></pre>
  
  <p>Using BRE I get no output from BSD <code>grep</code>.</p>
  
  <p><strong>ERE Attempt</strong> (succeeds)<strong>:</strong></p>
  
  <pre><code>$ grep -oE "hel.*?o" &lt;&lt;&lt; "$string"
  hello
  </code></pre>
  
  <p>After enabling the <code>-E</code> option, I am <strong>surprised</strong> that I was able to extract my desired output. <strong>My question is on the output I am getting from this attempt.</strong> </p>
  
  <p><strong>PCRE Attempt</strong> (fails)<strong>:</strong></p>
  
  <pre><code>$ grep -oP "hel.*?o" &lt;&lt;&lt; "$string"
  usage: grep [-abcDEFGHhIiJLlmnOoPqRSsUVvwxZ] [-A num] [-B num] [-C[num]]
      [-e pattern] [-f file] [--binary-files=value] [--color=when]
      [--context[=num]] [--directories=action] [--label] [--line-buffered]
      [--null] [pattern] [file ...]
  </code></pre>
  
  <p>Using <code>-P</code> option gave me usage error which was expected since BSD option of <code>grep</code> does not support PCRE. </p>
  
  <p>So my question is why would using ERE on BSD <code>grep</code> yield correct output with using non-greedy quantifier but not with GNU <code>grep</code>. </p>
  
  <p>Is this a bug, an un-documented feature of BSD <code>egrep</code> or my mis-understanding of the output?</p>