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