in reply to Help Needed to parse phone numbers

In my experience, capturing all possible variants of phone numbers in a single regex is impossible, so I would use a battery of expressions. I assume that if multiple phone numbers are found in your record, that they are separated by commas. This code should do the trick. Note that I used ^ and $ in the expressions, because the office phone pattern is a proper subset of the international pattern, and both numbers match the office pattern if I do not insist that the pattern matches the whole string. Sometimes you need to sort your patterns carefully according to priority if multiple patterns match the same string.
#!/usr/bin/perl my @office_patterns = ('^\d{3}-\d{4}$'); my @international_patterns = ('^[+]\d \d{3} \d{3}-\d{4}$'); my $record = "OfficePhoneNumber: 662-5555,+1 102 892-1314"; my ($field, $data) = split(/:\s*/, $record); my @office_phones; my @international_phones; NUMBER: foreach $number (split(/,\s*/, $data)) { # Trim spaces. $number =~ s/^\s+//; $number =~ s/\s+$//; foreach $office_pattern (@office_patterns) { if ($number =~ /$office_pattern/) { push @office_phones, $number; next NUMBER; } } foreach $international_pattern (@international_patterns) { if ($number =~ /$international_pattern/) { push @international_phones, $number; next NUMBER; } } } print "Office #: ", join(", ", @office_phones), "\n"; print "International #: ", join(", ", @international_phones), "\n";

Replies are listed 'Best First'.
Re^2: Help Needed to parse phone numbers
by Anonymous Monk on May 18, 2009 at 18:10 UTC
    Thanks for the script. I have modified the script a bit as per my need. However, one thing I dont want which the script is doing. The script is picking up "662-5555" even if it is at second place like, my $record = "OfficePhoneNumber: ,+1 102 892-1314,662-5555"; Ideally, if "662-5555" is at the second place, the script should ignore it. Can you help me in this? Regards, Prince