[Pdx-pm] regex & phone numbers...
n/a
nforrett at wgz.com
Thu Jul 25 14:47:42 CDT 2002
On Thu, 25 Jul 2002, Kari Chisholm wrote:
> I want to convert the actual seven- or ten-digit phone number part to
> just xxx-xxx-xxxx. I also want to leave alone anything that comes
> after that - which is obviously the tough part. The logic should be
> basically this: just process through the number left to right,
> grabbing the first seven or ten numbers, then reformat those and tack
> on whatever's left. The challenge is figuring out when it's a
> seven-digit or a ten-digit number.
Here is a start...
$line =~ s/(?:
\(? # optional leading paren
(\d{3}) # capture area code
\)? # optional closing paren
[-\s.]? # optional hyphen, space or period
)? # make the whole area code bit optional
(\d{3}) # capture first three digits
[-\s.]? # optional hyphen, space or period
(\d{4}) # capture last four digits
/(\1) \2-\3/x ;
(503) 123-4567 => (503) 123-4567
503.123.4567 => (503) 123-4567
(503)-123-4567 => (503) 123-4567
123-4567 => () 123-4567
503 123 4567 => (503) 123-4567
503-123-4567 ext. 89 => (503) 123-4567 ext. 89
123-4567 ext. 89 => () 123-4567 ext. 89
503-123-4567-mom's house => (503) 123-4567-mom's house
Plus a few extra cases...
asdf1234567fdsa => asdf() 123-4567fdsa
asdf1234567890fdsa => asdf(123) 456-7890
asdf 12345678 fdsa => asdf () 123-45678 fdsa
This assumes you don't mind the extra parens in the case of a missing area
code. Though Christian probably had the better suggestion. =)
> I've conceptualized any number of highly complex and idiotic ways of doing
> this. I'm just wondering if there's a simpler regex approach to this...
> Any ideas?
-- Nick
,--< Nick Forrette >--------------------------.
| e-mail: nforrett at wgz.org |
| www : http://www.drforehead.net/ |
`---------------------------------------------'
umop apisdn aje noh
More information about the Pdx-pm-list
mailing list