Dear Monks:

I found an interesting case of regex. This is actually from Programing Perl 4th errata.
The output of the following line of code is different than what is des +cribed in the text: "abcdef" =~ / .+ (?{say "Matched so far: $&"}) bcdef $/x; The text states that this prints the following: Matched so far: abcdef Matched so far: abcde Matched so far: abcd Matched so far: abc Matched so far: ab Matched so far: a But I am finding that only the last line gets printed: Matched so far: a One solution to get the output that the text describes is to slightly +modify the code example to the following: "abcdef" =~ / .+ (?{say "Matched so far: $&"}) .cdef $/x;
I am wondering why "backtrack" doesn't behave the way originally expected and why the edited version works?

Thank you!

In reply to 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.