Parsing in regular expression

S

sam

Hi,

The following pattern does not return all the values, the pattern only
able to parse the first two fields:

my $test = "01-, Revlon, n/a, Revlon";
$test =~
/^([0-9]*-), #prefix 1
(.*), #vendor 2
(.*), #Category 3
(.*) #basename 4
/x;

if ($1 eq "" or $2 eq "" or $3 eq "") {
print "Failed to parse $test\n";
} else {
# get the value
}

What should be the correct implementation of this pattern?
Thanks
Sam
 
A

Arndt Jonasson

sam said:
The following pattern does not return all the values, the pattern only
able to parse the first two fields:

my $test = "01-, Revlon, n/a, Revlon";
$test =~
/^([0-9]*-), #prefix 1
(.*), #vendor 2
(.*), #Category 3
(.*) #basename 4
/x;

if ($1 eq "" or $2 eq "" or $3 eq "") {
print "Failed to parse $test\n";
} else {
# get the value
}

It works for me - I get the respective fields in all $1-$4.
" n/a" in $3, in particular. What do you get in $3?

It's better to test the match itself for success:
if ($test =~ ...)
but that doesn't explain why the match doesn't work for you.
 
T

Tad McClellan

The following pattern does not return all the values, the pattern only
able to parse the first two fields:

my $test = "01-, Revlon, n/a, Revlon";

What should be the correct implementation of this pattern?

my @fields = split /,\s*/, $test;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top