See perlvar#$.
rxrx and http://perldoc.perl.org/re.html#%27debug%27-mode and other regex tools
The "anchor" misnomer in regexes (string location assertion)
Why \n matches but not $^?
Disabling regexp optimizations?

matches after newline (or beginning of string). $ matches before newline (or end of string)

$ perl -MData::Dump -Mre=debug -le " dd( $_=qq{a\n\nb} ); s{^$}{boop}m +; dd( $_ ); " Compiling REx "^$" Final program: 1: MBOL (2) 2: MEOL (3) 3: END (0) anchored ""$ at 0 anchored(MBOL) minlen 0 "a\n\nb" Matching REx "^$" against "a%n%nb" 0 <> <a%n%nb> | 1:MBOL(2) 0 <> <a%n%nb> | 2:MEOL(3) failed... 2 <a%n> <%nb> | 1:MBOL(2) 2 <a%n> <%nb> | 2:MEOL(3) 2 <a%n> <%nb> | 3:END(0) Match successful! "a\nboop\nb" Freeing REx: "^$"

Trying to match newline after end of line won't work, $\n won't work

$ perl -MData::Dump -Mre=debug -le " dd( $_=qq{a\n\nb} ); s{^$\n}{boop +}m; dd( $_ ); " "a\n\nb" Compiling REx "^%nn" Final program: 1: MBOL (2) 2: EXACT <\nn> (4) 4: END (0) anchored "%nn" at 0 (checking anchored) anchored(MBOL) minlen 2 Guessing start of match in sv for REx "^%nn" against "a%n%nb" Did not find anchored substr "%nn"... Match rejected by optimizer "a\n\nb" Freeing REx: "^%nn"

But matching an OPTIONAl newline works

$ perl -MData::Dump -Mre=debug -le " dd( $_=qq{a\n\nb} ); s{^$\n?}{boo +p}ms; dd( $_ ); " "a\n\nb" Compiling REx "^%nn?" Final program: 1: MBOL (2) 2: EXACT <\n> (4) 4: CURLY {0,1} (8) 6: EXACT <n> (0) 8: END (0) anchored "%n" at 0 (checking anchored) anchored(MBOL) minlen 1 Guessing start of match in sv for REx "^%nn?" against "a%n%nb" Found anchored substr "%n" at offset 1... Found /^/m, restarting lookup for check-string at offset 2... Found anchored substr "%n" at offset 2... Position at offset 2 does not contradict /^/m... Guessed: match at offset 2 Matching REx "^%nn?" against "%nb" 2 <a%n> <%nb> | 1:MBOL(2) 2 <a%n> <%nb> | 2:EXACT <\n>(4) 3 <a%n%n> <b> | 4:CURLY {0,1}(8) EXACT <n> can match 0 times out of 1 +... 3 <a%n%n> <b> | 8: END(0) Match successful! "a\nboopb" Freeing REx: "^%nn?"


In reply to Re^3: Why multiline regex doesn't work? ( ^$\n? anchors are "string location assertion") by Anonymous Monk
in thread Why multiline regex doesn't work? by nbd

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.