Perl Regex / String / Split

I need to be able to take a string and split it into two values.  Basically, I need two values from the string, however,  I need to "weed out" the stuff I don't need and JUST get the values needed.  Here's what the string looks like:

Image Width: 1419 Image Length: 1001

See code snippet for my current set of code.

Right now the output is showing as follows:

# ./test3.pl
First:
Second: 1419

What I need the output to be
# ./test3.pl
First: 1419
Second: 1001

So, I actually need the value of "Image Width" to be the first array value, and the value of "Image Length" to be the second array value.  

Thanks in advanced.
#!/usr/bin/perl
$r = "Image Width: 1419 Image Length: 1001";

@r = split(/: /,$r);
$r[0] =~ s/\D//g;
$r[1] =~ s/\D//g;

print "First: ".$r[0];
print "\n";
print "Second: ".$r[1];
print "\n";

Open in new window

LVL 2
drewrockshardAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Terry WoodsIT GuruCommented:

#!/usr/bin/perl
$r = "Image Width: 1419 Image Length: 1001";

@r = split(/(?<=\d)\D/,$r);
$r[0] =~ s/\D//g;
$r[1] =~ s/\D//g;

print "First: ".$r[0];
print "\n";
print "Second: ".$r[1];
print "\n";

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zlobchoCommented:

#!/usr/bin/perl
$r = "Image Width: 1419 Image Length: 1001";
$r=~/.*:\s*(\w*)\s*.*:\s*(\w*)/;
print "First: $1\nSecond: $2\n";

Open in new window

0
TintinCommented:

#!/usr/bin/perl

$r = "Image Width: 1419 Image Length: 1001";
my ($width,$length) = $r =~ /:\s*(\d+).*:\s*(\d+)/;
print "first $width\n";
print "second $length\n";

Open in new window

0
FerrostiCommented:
Just for explanation...
Your split() takes everything until the first occurence of your delimiter ": ", content "Image Width". Since it does not contain any decimals $r[0] is empty at line 5.
0
drewrockshardAuthor Commented:
Awesome; this worked great!  
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.