How does the engine know that it has already succeeded with the fourth regex (when pos is already 3)? There must be some mechanism that records that internally in the regex engine.

It might be informative to look at re debugging (although the particular state you're asking about isn't displayed, unfortunately):

use re 'debug'; my $str = "abc"; $str =~ m/.|$/g for 1..5; __END__ Compiling REx ".|$" Final program: 1: BRANCH (3) 2: REG_ANY (5) 3: BRANCH (FAIL) 4: SEOL (5) 5: END (0) minlen 0 Matching REx ".|$" against "abc" 0 <> <abc> | 0| 1:BRANCH(3) 0 <> <abc> | 1| 2:REG_ANY(5) 1 <a> <bc> | 1| 5:END(0) Match successful! Matching REx ".|$" against "bc" 1 <a> <bc> | 0| 1:BRANCH(3) 1 <a> <bc> | 1| 2:REG_ANY(5) 2 <ab> <c> | 1| 5:END(0) Match successful! Matching REx ".|$" against "c" 2 <ab> <c> | 0| 1:BRANCH(3) 2 <ab> <c> | 1| 2:REG_ANY(5) 3 <abc> <> | 1| 5:END(0) Match successful! Matching REx ".|$" against "" 3 <abc> <> | 0| 1:BRANCH(3) 3 <abc> <> | 1| 2:REG_ANY(5) | 1| failed... 3 <abc> <> | 0| 3:BRANCH(5) 3 <abc> <> | 1| 4:SEOL(5) 3 <abc> <> | 1| 5:END(0) Match successful! Matching REx ".|$" against "" 3 <abc> <> | 0| 1:BRANCH(3) 3 <abc> <> | 1| 2:REG_ANY(5) | 1| failed... 3 <abc> <> | 0| 3:BRANCH(5) 3 <abc> <> | 1| 4:SEOL(5) 3 <abc> <> | 1| 5:END(0) END: Match possible, but length=0 is smaller than requested=1, failing +! | 0| BRANCH failed... Match failed Freeing REx: ".|$"
my question is whether the state that regex uses to determine whether $ was already matched is available to me? ... I would like to understand if the internal structure that has this information ($ matched) is accessible

I have the same question as the others: Why? The regex engine seems to be DWIMing just fine...

I don't know of a way to get at the internal information you're asking about. You can of course simply ask the engine to tell you what it matched, e.g.:

my $str = "abc"; print $str =~ m/(.)|$/g ? "matched! ".($1//'$') : 'no match', "\n" for 1..5; __END__ matched! a matched! b matched! c matched! $ no match

In reply to Re^5: perl indication of end of string already matched by haukex
in thread perl indication of end of string already matched by nachumk

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.