user3494614
user3494614

Reputation: 603

Why in perl this regex is not matching line

I made some changes to perl code and I am not able to understand why below regular expression is not matching against the input line.

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";
}

Thanks In Advance

Upvotes: 0

Views: 62

Answers (2)

ikegami
ikegami

Reputation: 386621

Because 0 doesn't match [1-9]\d*.


Have you considered using the following:

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

Upvotes: 6

AnFi
AnFi

Reputation: 10913

Your test string does not match the regular expression.

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

0 does not match ([1-9]\d*)


Make you regex simpler by using qr operator.

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

Upvotes: 3

Related Questions