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.
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.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 ]]
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:
(There is no warning because $[ has a default initialized value.)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 ]]
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:
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.)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 ]]
Further Update: The interpolation of $[ can be clearly seen here:
The default value of $[ is 0;c:\@Work\Perl\monks>perl -wMstrict -e "my $rx = qr{$[.\n]*}m; print $ +rx;" (?^m:0.\n]*)
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |