स्ट्रिंग के लिए परिणाम "ए \ nbb \ ncc" के लिए परिणाम नहीं समझा जा सकता है, मैं अंतिम अक्षर ("ए") के अंत तक अंतिम पत्र से मिलान करना चाहता हूं बहु लाइन स्ट्रिंग और उम्मीद है कि
"aa\nbb\ncc" =~ qr/(. $ .+)/xms
से मेल खाता है a\nbb\ncc
और उस
"aa\nbb\ncc\n" =~ qr/(. $ .+)/xms
मैचों a\nbb\ncc\n
।
लेकिन मैं "aa\nbb\ncc" =~ qr/(. $ .+)/xms
के लिए कोई मुकाबला नहीं मिला है और "aa\nbb\ncc" =~ qr/(. $ .+)/xms
के लिए c\n
मेल खाते हैं।
qr/(. $ ..+)/xms
का उपयोग करके मुझे अपेक्षित परिणाम मिल गए (उदाहरण कोड देखें)।
पर्ल संस्करण 5.14.2।
क्या कोई उस व्यवहार के लिए स्पष्टीकरण दे सकता है?
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
निम्न उदाहरण कोड चल रहा है:
#!/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;
}
}
उत्पादन होता है:
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
<
यह पर्ल में एक बग जैसा लगता है। अच्छा खोज! संबंधित: '" एक \ nb "' 'm/a $ .. \ z/ms' के बजाय' m/a $ ... \ z/ms' मिलान करता है; * लेकिन *, जब मैं क्या हो रहा है, यह जानने के लिए बिंदुओं के चारों ओर कोष्ठक जोड़ता हूं, तो यह अचानक 'm/a $ (।) (।) \ z/ms' के बजाय' m/a $ (।) से मेल खाता है (।) (।) \ z/ms'। – ruakh
बल्कि अजीब। 5.12.2 में समान है। – katastrophos