Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regualr Expressions Help

Status
Not open for further replies.

FreeBASE

Programmer
May 3, 2002
39
US
How can I format a number like- 5553231123

to

(555)323-1123

using regular expressions?

Thanks, many times in advance..
 
$phone = '5553231123';
$phone =~ s/(...)(...)(.*)/($1)$2-$3/;
print "$phone\n";
Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884
 
Code:
$number = "5551231234";
$number =~ s/\D//g;  # for good measure
$rev_number = join('', reverse ( split(//, $number) ) );
$rev_number =~ /(....)(...)(...)?(.)?/;
unshift @nums, join('',reverse (split(//, $_))) for($1,$2,$3,$4);

print "$nums[0] ($nums[1]) $nums[2]-$nums[3]";

Hope this helps...
--jim * * * Merry Christmas and Happy New Year! * * *

( Don't touch that Red Flag link you crazy freak, it's just a holiday greeting! )
 
Same time Mike ;)

I kind of took it to the next level anyways, works with all kinds of input possibilities. Allthough - that was not requested.

Maybe I'll have better luck next time :p

--jim * * * Merry Christmas and Happy New Year! * * *

( Don't touch that Red Flag link you crazy freak, it's just a holiday greeting! )
 
Ok <peering myopically at screen> .....

You want to comment that a bit so my head will stop hurting? <grin>

(looks good actually) Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884
 
This is what I'm thinking..
you reversed the string to start matching from the end in case the phone number has a leading &quot;1&quot; so it won't become part of the area code as it would if you just grabbed the first 3 numbers?
 
Pretty much. And the phone number could've come formatted with dashes and stuff already, that's why I strip out NON-numbers at the beginning. Also, it will take input without an area code. Like if it were for a personal phone book or something, and you didn't need the area code for all entries.

Mike - sorry about your head hurting, but I think that you understanding that snippet is not a problem for you at all. Rather - some advil might be the order of the day!
On another note: notice the $MerryChristmas post is missing??? It was removed becuase it was &quot;red flagged&quot; too many times... hence my new signature. :p

--jim
* * * Merry Christmas and Happy New Year! * * *

( Don't touch that Red Flag link you crazy freak, it's just a holiday greeting! )
 
Red Flags :) you've gotta love 'em. Mike

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884
 
Code:
$number =~ /

  [[:blank:].:+\-]*?		# seperators
  (\(?(?i:fax|phone)?\)?)	# type of number
  [[:blank:].:+\-]*?		# seperators
  ((?:\(?\d{1,2}\)?)?)		# country code
  [[:blank:].:+\-]*?		# seperators

  (                             # area code
   (?:							
    [[:blank:].:+\-()]*?	# separators
    \d		  	        # number
    [[:blank:].:+\-()]*? 	# separators
   ){3}		   	        # three times
  )

  (                             # prefix
   (?:							
    [[:blank:].:+\-()]*?	# separators
    \d		  	        # number
    [[:blank:].:+\-()]*? 	# separators
   ){3}		   	        # three times
  )

  (                             # line number
   (?:							
    [[:blank:].:+\-()]*?	# separators
    \d		  	        # number
    [[:blank:].:+\-()]*? 	# separators
   ){4}		   	        # three times
  )

  (?i:                          # extension
   [[:blank:].:+\-]*?
   (?:x|ext)
   [[:blank:].:+\-]*?
   (\d+?(?!\d))
  )?
 \s*?
 /xi;

print &quot;$1: &quot; if $1; # print the type if it exists
print &quot;$2 &quot; if $2;  # print the country code if it exists
print &quot;($3)$4-$5&quot;;  # print the number
print &quot; x$6&quot; if $6; # print the extension if it exists
Sincerely,

Tom Anderson
CEO, Order amid Chaos, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top