CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
Phone Number parsing Help

 



rohan076
Novice

Sep 15, 2008, 9:44 PM

Post #1 of 6 (3794 views)
Phone Number parsing Help Can't Post

This is the code I have



#!/usr/local/bin/perl -w

my $a="123-3456, 895-4839484, 84938932-4483938, 489-2834";

while ( $a =~ /(\d{3}-\d{4})/g ) {

print "$1\n";
}



And when I run the program I get the following

C:\>perl phone.pl
123-3456
895-4839
932-4483
489-2834
C:\>



I just want to pick numbers of the format ddd-dddd, how do I do that. in other words, i want the result to be only 123-3456 and 489-2834



Thanks for your help.


KevinR
Veteran


Sep 15, 2008, 10:28 PM

Post #2 of 6 (3790 views)
Re: [rohan076] Phone Number parsing Help [In reply to] Can't Post

split the string into a list:


Code
my @nums = split(/,\s*/,$a);


then check each element of the array including string anchors in the regexp:


Code
foreach my $phone (@nums) { 
print "$phone\n" if ($phone =~ /^\d{3}-\d{4}$/;
}

-------------------------------------------------


rohan076
Novice

Sep 16, 2008, 8:42 AM

Post #3 of 6 (3760 views)
Re: [KevinR] Phone Number parsing Help [In reply to] Can't Post

Thanks Kevin, the string I gave was an example. a more real world example would be

$a = "Hey, my number is 382-4839, my son's number is 483-28378 and my wife's number is 38892-4839, call us anytime"

in this case the result would be
382-4839
483-2837
892-4839

i want it to be only 382-4839, is there something like \B which will tell the code to match ONLY XXX-XXXX numbers


KevinR
Veteran


Sep 16, 2008, 11:11 AM

Post #4 of 6 (3750 views)
Re: [rohan076] Phone Number parsing Help [In reply to] Can't Post


Code
$a = "Hey, my number is 382-4839, my son's number is 483-28378 and my wife's number is 38892-4839, call us anytime" ; 
my @nums = ($a =~ /\b(\d{3}-\d{4})\b/g);
print "$_\n" for @nums;

-------------------------------------------------


rohan076
Novice

Sep 16, 2008, 3:53 PM

Post #5 of 6 (3738 views)
Re: [KevinR] Phone Number parsing Help [In reply to] Can't Post

Thanks Kevin, I got what I wanted.


witnesstheday
New User

Dec 3, 2008, 12:49 PM

Post #6 of 6 (3208 views)
Re: [rohan076] Phone Number parsing Help [In reply to] Can't Post

This minor adjustment to the original code also sort of works:

Code
my $a="123-3456, 895-4839484, 84938932-4483938, 489-2834"; 

while ( $a =~ /(\d{3}-\d{4}\D)/g ) {

print "$1\n";
}


i.e. sticking \D at the end, so it's followed by a nondigit
I had one at the start too, but it didn't count \A as \D, and if
one put in \A it'd miss ones hypothetically later

you could have two if you wanted to include the start,
i.e. one with \D and one with \A



Code
#!/usr/bin/perl 


my $a="123-3456, 895-4839484, 345-2323 84938932-4483938, 489-2834";


while ( $a =~ /(\D\d{3}-\d{4}\Z)/g){

print "$1\n";
}

while ( $a =~ /(\A\d{3}-\d{4}\D)/g){

print "$1\n";
}


while ( $a =~ /(\D\d{3}-\d{4}\D)/g){

print "$1\n";
}


is a painful but working way through, I found

however now I have tried the \b, and I have realised that obviously that's the best way, I just never used \b's enough so far.

Still, my way works too, but is a bit messy.

Just thought I'd have a go.


(This post was edited by witnesstheday on Dec 3, 2008, 1:02 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives