Perl qr引用正则表达式的行为不同



我的代码:

my $orderId = "1610209";
$pdfFilename = "1610209_1.pdf";
my $re = qr/^${orderId}_/;
print Dumper($re);
if($pdfFilename =~ $re) {
    print "matched";
}

不匹配!!$VAR1 = qr/(?^:^1610209_)/;

怎么了?我的环境中:

perl - v

这是perl 5,版本18,subversion 2 (v5.18.2)构建为x86_64-linux-gnu-thread-multi

lsb_release——

发行商ID: Ubuntu描述:Ubuntu 14.04.2 LTS版本:14.04代号:可靠的

适合我!™

你用的是Windows吗?由于STDOUT(即。印刷到屏幕)通常是行缓冲的。这意味着它在看到换行符之前不会显示。如果这是程序的最后一行,它可能在窗口关闭之前永远不会显示。

试试print "matchedn";

有关更多信息,请参阅

use strict;
use warnings;
use Data::Dumper;
my $orderId = "1610209";
my $pdfFilename = "1610209_1.pdf";
my $re = qr/^${orderId}_/;
if($pdfFilename =~ $re) {
print "matched Dumper($re)n";
} else {
print "not matched Dumper ($re)n";
}

返回

matched Dumper((?^:^1610209))
但是,为什么要使用数据转储器呢?

use strict;
use warnings;
my $orderId = "1610209";
my $pdfFilename = "1610209_1.pdf";
if($pdfFilename =~ /^$orderId/) {
    print "Matched $orderId with $pdfFilenamen";
} else {
    print "Not Matched $orderId with $pdfFilenamen";
}

代码匹配。


你确定你实际上没有以下内容吗?

my $orderId = "1610209n";

chomp行,从文件中读取以删除尾随的换行符

最新更新