If 'm' as flag causes ^ and $ to gain magic as in

m Treat string as multiple lines. That is, change "^" and "$" from matching the start or end of the string to matching the start or end of any line anywhere within the string.

and $ matches end-of-line (or before newline) as in

           $   Match the end of the line (or before newline at the end)

It might be arguable that the newline would be matched by $ in

$ perl -Mre=debug -wle'"ABC\nDEF" =~ m/C(?:$)^D/m' Freeing REx: `","' Compiling REx `C(?:$)^D' size 7 Got 60 bytes for offset annotations. first at 1 rarest char D at 1 1: EXACT <C>(3) 3: MEOL(4) 4: MBOL(5) 5: EXACT <D>(7) 7: END(0) anchored "CD" at 0 (checking anchored) minlen 2 Offsets: [7] 1[1] 0[0] 5[1] 7[1] 8[1] 0[0] 9[0] Omitting $` $& $' support. EXECUTING... Guessing start of match, REx "C(?:$)^D" against "ABC DEF"... Did not find anchored substr "CD"... Match rejected by optimizer Freeing REx: `"C(?:$)^D"'

I understand that it doesn't. Making the newline explicit as in tye's example does

$ perl -Mre=debug -wle'"ABC\nDEF" =~ m/C(?:$)$\?^D/m' Freeing REx: `","' Omitting $` $& $' support. EXECUTING... Compiling REx `C(?:$) ?^D' size 11 Got 92 bytes for offset annotations. first at 1 rarest char D at 0 rarest char C at 0 1: EXACT <C>(3) 3: MEOL(4) 4: CURLY {0,1}(8) 6: EXACT <\n>(0) 8: MBOL(9) 9: EXACT <D>(11) 11: END(0) anchored "C"$ at 0 floating "D" at 1..2 (checking floating) minlen 2 Offsets: [11] 1[1] 0[0] 5[1] 8[1] 0[0] 7[1] 0[0] 9[1] 10[1] 0[0] 11[0] Guessing start of match, REx "C(?:$) ?^D" against "ABC DEF"... Found floating substr "D" at offset 4... Found anchored substr "C"$ at offset 2... Starting position does not contradict /^/m... Guessed: match at offset 2 Matching REx "C(?:$) ?^D" against "C DEF" Setting an EVAL scope, savestack=6 2 <AB> <C DEF> | 1: EXACT <C> 3 <ABC> < DEF> | 3: MEOL 3 <ABC> < DEF> | 4: CURLY {0,1} EXACT <\n> can match 1 times out of 1... Setting an EVAL scope, savestack=6 4 <ABC > <DEF> | 8: MBOL 4 <ABC > <DEF> | 9: EXACT <D> 5 <ABC D> <EF> | 11: END Match successful! Freeing REx: `"C(?:$)\n?^D"'

In reply to Re^2: Why \n matches but not $^? (weight) by procura
in thread Why \n matches but not $^? by tel2

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.