use strict;
my $type = "build";
if ($type =~ (/build|test/))
{
print "type=$1";
}
我期望它打印「type = build」,但是$ 1沒有得到任何東西,它打印出「type =」,我做錯了什麼?用Perl模式打印匹配的字符串匹配
use strict;
my $type = "build";
if ($type =~ (/build|test/))
{
print "type=$1";
}
我期望它打印「type = build」,但是$ 1沒有得到任何東西,它打印出「type =」,我做錯了什麼?用Perl模式打印匹配的字符串匹配
它看起來像你沒有捕捉您的括號什麼,
perl -MO=Deparse -e'
use strict;
my $type = "build";
if ($type =~ (/build|test/))
{
print "type=$1";
}
'
輸出
use strict;
my $type = 'build';
if ($type =~ /build|test/) {
print "type=$1";
}
但/(build|test)/
應該完全是另一回事。
這就是人們在這裏建議使用use warnings
和use strict
的原因。 如果你在你的代碼添加use warnings
,你會得到一個警告:
Use of uninitialized value $1 in concatenation (.) or string at type.pl line 7
代碼:
use warnings;
use strict;
my $type = "build";
if ($type =~ /(build|test)/)
{
print "type=$1";
}
我認爲這篇文章可以幫助你得到一個答案:http://stackoverflow.com/ question/24936145/perl-empty-1-regex-value-when-matching – Birei 2015-03-02 22:50:03
提示:'使用警告' – TLP 2015-03-02 22:57:47