13
For string "aa\nbb\ncc" I want to match from the last letter left to the first newline ("a") to the end of the multi line string and expected that
"aa\nbb\ncc" =~ qr/( . $ .+ )/xms matches a\nbb\ncc
and that
"aa\nbb\ncc\n" =~ qr/( . $ .+ )/xms matches a\nbb\ncc\n.
But I got no match for "aa\nbb\ncc" =~ qr/( . $ .+ )/xms
and match c\n for "aa\nbb\ncc" =~ qr/( . $ .+ )/xms.
Using qr/( . $ ..+ )/xms I got the expected results (see example code).
Perl version 5.14.2.
Can anyone give an explanation for that behaviour?
perldoc perlre:
m Treat string as multiple lines. That is, change "^" and "$"
from matching the start or end of the string to matching the start
or end of any line anywhere within the string.
s Treat string as single line. That is, change "." to match any character
whatsoever, even a newline, which normally it would not match.
Used together, as "/ms", they let the "." match any character whatsoever,
while still allowing "^" and "$" to match, respectively, just after and
just before ewlines within the string.
\z Match only at end of string
Running the following example code :
#!/usr/bin/env perl
use strict;
use warnings;
print "Multiline string : ", '"aa\nbb\ncc"', "\n\n";
my $str = "aa\nbb\ncc";
print_match($str, qr/( . $ )/xms); # matches "a"
print_match($str, qr/( . $ . )/xms); # matches "a\n"
print_match($str, qr/( . $ .. )/xms); # matches "a\nb"
print_match($str, qr/( . $ ..+ )/xms); # matches "a\nbb\ncc"
print_match($str, qr/( . $ .+ )/xms); # NO MATCH ! Why ???
print_match($str, qr/( . $ .+ \z )/xms); # NO MATCH ! Why ???
print "\nMultiline string now with terminating newline : ", '"aa\nbb\ncc\n"', "\n\n";
$str = "aa\nbb\ncc\n";
print_match($str,