为什么在Perl中此正则表达式不匹配行

Why in perl this regex is not matching line

我对perl代码进行了一些更改,但我不明白为什么下面的正则表达式与输入行不匹配。

1
2
3
4
5
6
my $regex='^(780200703303)\\\\s+(\\\\d+)\\\\s+(\\\\d+)\\\\s+(\\\\d+)\\\\s+(\\\\d+)\\\\s+(\\\\d+)\\\\s+(\\\\d+)\\\\s+(\\\\d+)\\\\s+(\\\\d+)\\\\s+([1-9]\\\\d*)\\\\s+([1-9]\\\\d*)\\\\s+$';
my $line='780200703303    2            0            3            0            0            0            0            0            0            1 ';
if ( $line =~ m/$regex/ )
{
    print"Matched";
}

在此先感谢


因为0[1-9]\\d*不匹配。

您是否考虑过使用以下内容:

1
2
3
4
my @fields = split ' ', $line;
if ($fields[0] == 780200703303) {
   ...
}

您的测试字符串与正则表达式不匹配。

1
2
my $regex='\\\\s+([1-9]\\\\d*)\\\\s+([1-9]\\\\d*)\\\\s+$';
my $line='            0            1 ';

0([1-9]\\d*)

不匹配

通过使用qr运算符使您的正则表达式更简单。

1
my $regex= qr/\\s+([1-9]\\d*)\\s+([1-9]\\d*)\\s+$/;