Dla ciągu "aa \ nbb \ ncc" Chcę dopasować od ostatniej litery lewej do pierwszej nowej linii ("a") do końca oczekuje multi ciąg linii i że
"aa\nbb\ncc" =~ qr/(. $ .+)/xms
mecze a\nbb\ncc
i że
"aa\nbb\ncc\n" =~ qr/(. $ .+)/xms
mecze a\nbb\ncc\n
.
Ale nie udało mi się zdobyć "aa\nbb\ncc" =~ qr/(. $ .+)/xms
i dopasować c\n
dla "aa\nbb\ncc" =~ qr/(. $ .+)/xms
.
Używanie qr/(. $ ..+)/xms
Mam oczekiwane wyniki (zobacz przykładowy kod).
Wersja perl 5.14.2.
Czy ktoś może podać wyjaśnienie tego zachowania?
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 Poniższy przykład kodu:
#!/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, 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\n"
print_match($str, qr/(. $ .+)/xms); # MATCHES "c\n" ! Why ???
print_match($str, qr/(. $ .+ \z)/xms); # MATCHES "c\n" ! Why ???
sub print_match {
my ($str, $regex) = @_;
$str =~ $regex;
if ($1) {
printf "--> %-20s matched : >%s< \n", $regex, $1;
}
else {
printf "--> %-20s : no match !\n", $regex;
}
}
wyjście jest:
Multiline string : "aa\nbb\ncc"
--> (?^msx:(. $)) matched : >a<
--> (?^msx:(. $ .)) matched : >a
<
--> (?^msx:(. $ ..)) matched : >a
b<
--> (?^msx:(. $ ..+)) matched : >a
bb
cc<
--> (?^msx:(. $ .+)) : no match !
Multiline string now with terminating newline : "aa\nbb\ncc\n"
--> (?^msx:(. $)) matched : >a<
--> (?^msx:(. $ .)) matched : >a
<
--> (?^msx:(. $ ..)) matched : >a
b<
--> (?^msx:(. $ ..+)) matched : >a
bb
cc
<
--> (?^msx:(. $ .+)) matched : >c
<
Wydaje się, że jest to błąd w Perlu. Dobre znalezisko! Zależnie: '' a \ nb "' dopasowuje 'm/a $ ... \ z/ms' zamiast' m/a $ .. \ z/ms'; * Ale *, kiedy dodać nawiasy wokół punktów, aby spróbować dowiedzieć się, co się dzieje, to nagle mecze 'm/a $ (.) (.) \ Oo/ms' zamiast' M/A $(). (.) (.) \ z/ms'. – ruakh
Raczej dziwnie. To samo dotyczy perl 5.12.2. – katastrophos