Why doesn’t my regular expression matching version number does not work?

I am using perl regular expressions to match a problem with a web script I have, and I have managed to put the behavior in a small snippet.

Use this Perl snippet in Perl 5.10.0 in Debian:

#!/usr/bin/perl
use warnings;
use strict;

my $line = "Version: 0\r\n";
my($version) = $line =~ m/^Version:(\s\d+)\r\ n$/;
print "1st failed \n" if not $version;
($version) = $line =~ m/^Version:\s(\d+)\r\n$/ ;
print "2nd failed \n" if not $version;
($version) = $line =~ m/^Version:\ (\d+)\r\n$/;
print "3th failed \n" if not $version;

With this output:

2nd failed
3th failed

Obviously, the only difference between the first and the second is to move the space out of the extracted pattern. Theoretically, the regular expression should not be modified, and only the returned part should be returned.

I don’t understand why The second and third are not exactly the same as the first one.

Edit:
If you delete the parentheses in $version are not the same script, you will not get the matching result, you will get the op’s Boolean result, get the matching result (only one string to match) tuple you need to receive it in one element.

The problem is that you are testing the boolean value true, because in the latter two cases, the string value you extracted is false (the string “0”). Try this:

< p>

$line = "Version: 0\r\n";
my $version;
($version) = $line =~ m/^Versi on:(\s\d+)\r\n$/;
print "1st failed \n" unless defined $version;
($version) = $line =~ m/^Version:\ s(\d+)\r\n$/;
print "2nd failed \n" unless defined $version;
($version) = $line =~ m/^Version:\ (\d+ )\r\n$/;
print "3th failed \n" unless defined $version;

I am using perl regular expressions to match what I have I’ve managed to put the behavior in a small snippet.

Use this Perl snippet for Perl 5.10.0 in Debian:

< /p>

#!/usr/bin/perl
use warnings;
use strict;

my $line = "Version: 0\r\n";
my($version) = $line =~ m/^Version:(\s\d+)\r\n$/;
print "1st failed \n" if not $version;
($version) = $line =~ m/^Version:\s(\d+)\r\n$/;
print "2nd failed \n" if not $version;
( $version) = $line =~ m/^Version:\ (\d+)\r\n$/;
print "3th failed \n" if not $version;

Has This output:

2nd failed
3th failed

Obviously, the only difference between the first and second is to move the space out of the extraction The pattern, theoretically should not modify the regular expression, only return the returned part.

I don’t understand why the second and third are not exactly the same as the first one.

Edit:
If you delete the parentheses in $version are not the same script, you will not get the matching result, you will get the op cloth You need to receive it in a tuple of matching results (only one string to match).

The problem is that you are testing the boolean value true, because in the latter two cases, the string value you extract is false (string “0”). Try this:

$line = "Version: 0\r\n";
my $version;
($version) = $line =~ m/^Version:(\s\d+)\r\n$/;< br />print "1st failed \n" unless defined $version;
($version) = $line =~ m/^Version:\s(\d+)\r\n$/;
print "2nd failed \n" unless defined $version;
($version) = $line =~ m/^Version:\ (\d+)\r\n$/;
print "3th failed \n" unless defined $version;

Leave a Comment

Your email address will not be published.