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).>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
If the regex is disambiguated as / \n $ \n /x (again, note the /x modifier), the regex matches both with and without the /m modifier.
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.>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'; " : :
Update: Consider the following misleading output from the OP:
In fact, neither regex matches: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
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.>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
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |