Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
当我运行时:
perl -e '$x="abc\nxyz\n123"; $x =~ s/\n.*/... multiline.../; printf("str %s\n", $x);'
我希望结果是:
str abc... multiline...
相反,我得到
str abc... multiline... 123
我哪里错了?
$x =~ s/\n.*/... multiline.../s
/s修饰符告诉 Perl 将匹配的字符串视为单行,这会导致.匹配换行符。通常它不会,导致您观察到的行为。
/s
.
您需要在正则表达式上使用 's' 修饰符,以便点 '.' 将匹配任何后续换行符。所以这:
$x =~ s/\n.*/... multiline.../;
变成这样:
$x =~ s/\n.*/... multiline.../s;