Because the first one is triggering an optimisation. If you do '.+b', you might expect the engine to naively just keep skipping non-newline chars (.) until it can't go any further, then fail to match a 'b', backtrack one position, still fail the 'b', then continue backtracking until only one '.' is consumed, and the following 'b' is matched.

Instead, the code that does '.+' is optimised to know that it must be followed by a known fixed string (bcdef) and avoids consuming so many characters that the constraint can't be met.

In the second case it isn't followed by a fixed string, but rather by a '.', so the optimisation isn't triggered, and the engine falls back to a naive series of backtracks.

Dave.


In reply to Re: Interesting behavior of regular expression engine by dave_the_m
in thread Interesting behavior of regular expression engine by lightoverhead

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.