In your second regex, you achieve no match because the regex expression  [.\n] does not mean what (I think) you think it means. There is also another problem with a predefined special variable  $[ that is being interpolated instead of the first part of the  $[.\n] regex expression you intended.

c:\@Work\Perl\monks>perl -le "use warnings; use strict; ;; my $s = qq{aaa : AAA\n} . qq{bbb : BBB\n} . qq}ccc : CCC\n} ; print qq{[[$s]]}; ;; my $m = 'bbb'; ;; my $t = $s =~ s/[.\n]*?^$m *: (.*)$[.\n]*/$1/rm ; ;; print qq{[[$t]]}; " [[aaa : AAA bbb : BBB ccc : CCC ]] [[aaa : AAA bbb : BBB ccc : CCC ]]
The  '.' (period) character is not special, i.e., not a metacharacter, in a  [] regex character class; it just matches a literal period, and there are no such characters in your  $s test string.

I'm not sure what the  [.\n] expression was intended to represent (maybe  [^\n] "anything but a newline"?), so I can't comment further until you can provide greater clarity. Note, however, that disambiguating the  $ metacharacter at least produces a different output, i.e., a match and substitution, even though the output is still not what you expect:

c:\@Work\Perl\monks>perl -le "use warnings; use strict; ;; my $s = qq{aaa : AAA\n} . qq{bbb : BBB\n} . qq}ccc : CCC\n} ; print qq{[[$s]]}; ;; my $m = 'bbb'; ;; my $t = $s =~ s/[.\n]*?^$m *: (.*)$(?:[.\n]*)/$1/rm ; ;; print qq{[[$t]]}; " [[aaa : AAA bbb : BBB ccc : CCC ]] [[aaa : AAABBBccc : CCC ]]
(There is no warning because  $[ has a default initialized value.)

Update: Note that the ambiguity of  $[.\n] (regex) and the  $[ predefined special variable (see perlvar) is yet another argument in favor of the  /x embedded whitespace regex modifier (other than simply being able to see the darn regex). Consider:

c:\@Work\Perl\monks>perl -le "use warnings; use strict; ;; my $s = qq{aaa : AAA\n} . qq{bbb : BBB\n} . qq{ccc : CCC\n} ; print qq{[[$s]]}; ;; my $m = 'bbb'; ;; my $t = $s =~ s/ [.\n]*? ^ $m [ ]* : [ ] (.*) $ [.\n]* /$1/xrm ; ;; print qq{[[$t]]}; " [[aaa : AAA bbb : BBB ccc : CCC ]] [[aaa : AAABBBccc : CCC ]]
Still not what you expected, but one less pitfall to negotiate. (The  [ ] expression is what I like to use to represent a space, where  \s represents any whitespace character, a larger set.)

Further Update: The interpolation of  $[ can be clearly seen here:

c:\@Work\Perl\monks>perl -wMstrict -e "my $rx = qr{$[.\n]*}m; print $ +rx;" (?^m:0.\n]*)
The default value of  $[ is 0;


Give a man a fish:  <%-(-(-(-<


In reply to Re: Why multiline regex doesn't work? by AnomalousMonk
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.