Here's the problem: With or without the /m modifier, the regex  /\n$\n/ does not match against the  "\n\n" string!
>perl -wMstrict -le "my $s = qq{\n\n}; print $s =~ /(\n$\n)/ ? qq{:$1:} : 'no match'; " no match >perl -wMstrict -le "my $s = qq{\n\n}; print $s =~ /(\n$\n)/m ? qq{:$1:} : 'no match'; " no match
The reason is that the  $\ sequence in the regex is taken as the  $\ 'output record separator' Perl special variable (a newline by default) and interpolated as such in the regex, which thus becomes equivalent to  / \n \n n /x (note the /x modifier).

If the regex is disambiguated as  / \n $ \n /x (again, note the /x modifier), the regex matches both with and without the /m modifier.

>perl -wMstrict -le "my $s = qq{\n\n}; print $s =~ /( \n $ \n )/x ? qq{:$1:} : 'no match'; " : : >perl -wMstrict -le "my $s = qq{\n\n}; print $s =~ /( \n $ \n )/xm ? qq{:$1:} : 'no match'; " : :
In many of the examples in other replies in this thread, the ambiguity of  $\ in a regex that arises from interpolation is not taken into account and causes (or can cause) confoosion.

Update: Consider the following misleading output from the OP:

string=<a\n> no modifier: regex=/^a$\n/ match => $ matches only boundary, \n matches newline [ ... ] m modifier (multi line mode): regex=/^a$\n/m match => $ matches only boundary, \n matches newline
In fact, neither regex matches:
>perl -wMstrict -le "my $s = qq{a\n}; print $s =~ /^a$\n/ ? ' ' : 'NO ', 'match'; print $s =~ /^a$\n/m ? ' ' : 'NO ', 'match'; " NO match NO match
The reason for the confusion is that the regex is first defined as  '^a$\n' (i.e., within non-interpolating single-quotes) in the test code, then interpolated within the actual  // regex operator, in which case the  $\ sequence is not ultimately interpolated as the output record separator string.

Again, after appropriate disambiguation, everything's fine:

>perl -wMstrict -le "my $s = qq{a\n}; print $s =~ /^ a $ \n/x ? ' ' : 'NO ', 'match'; print $s =~ /^ a $ \n/xm ? ' ' : 'NO ', 'match'; " match match

In reply to Re^3: When exactly do Perl regex's require a full match on a string? by AnomalousMonk
in thread When exactly do Perl regex's require a full match on a string? by ELISHEVA

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.