Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The presence of the <code>$</code> in the regular expression does not affect the greediness of the expression. It merely adds another condition which must be met for the overall match to succeed. </p> <p>Both <code>a+</code> and <code>a+?</code> are required to consume the first <code>a</code> they find. If that <code>a</code> is followed by more <code>a</code>'s, <code>a+</code> goes ahead and consumes them too, while <code>a+?</code> is content with just the one. If there were anything more to the regex, <code>a+</code> would be willing to settle for fewer <code>a</code>'s, and <code>a+?</code> would consume more, if that's what it took to achieve a match.</p> <p>With <code>a+$</code> and <code>a+?$</code>, you've added another condition: match at least one <code>a</code> <em>followed by</em> the end of the string. <code>a+</code> still consumes all of the <code>a</code>'s initially, then it hands off to the anchor (<code>$</code>). That succeeds on the first try, so <code>a+</code> is not required to give back any of its <code>a</code>'s.</p> <p>On the other hand, <code>a+?</code> initially consumes just the one <code>a</code> before handing off to <code>$</code>. That fails, so control is returned to <code>a+?</code>, which consumes another <code>a</code> and hands off again. And so it goes, until <code>a+?</code> consumes the last <code>a</code> and <code>$</code> finally succeeds. So yes, <code>a+?$</code> does match the same number of <code>a</code>'s as <code>a+$</code>, but it does so reluctantly, not greedily.</p> <p>As for the leftmost-longest rule that was mentioned elsewhere, that never did apply to Perl-derived regex flavors like Python's. Even without reluctant quantifiers, they could always return a less-then-maximal match thanks to <a href="http://www.regular-expressions.info/alternation.html" rel="nofollow">ordered alternation</a>. I think Jan's got the right idea: Perl-derived (or regex-directed) flavors should be called <a href="http://www.regular-expressions.info/engine.html" rel="nofollow">eager</a>, not greedy. </p> <p>I believe the leftmost-longest rule only applies to POSIX NFA regexes, which use NFA engines under under the hood, but are required to return the same results a DFA (text-directed) regex would.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload