Hi Monks,
I am trying to check for phone number input using the regular expression below, but I can't understand why it isn't matching, any help or suggestions in how I could improve my code to check phone number format like:
(555) 324-1233
555 - 324-1234
1 555 342-1234
It may or may not have spaces in between or dashes as well but looking at the regular expression that I have it should be very flexible in matching phone members.
Thanks for the help!
$phone_number = "(555) 324-1233";
if ($phone_number =~/^[(]?(d{0,3})[)]?[s]?[-]?(d{3})[s]?[-]?(d{4})[s]?
+[x]?(d*)$/){
#if ($phone_number =~/^(\D*\d{3}?)(\D*\d{3}?)(\D*\d{4}?)(\D*$)/){
#// we have a match, dump sub-patterns to $matches
$phone = $1; #// original number
$area_code = $2; #// 3-digit area code
$exchange = $3; #// 3-digit exchange
$number = $4; #// 4-digit number
$extension = $5; #// extension
print "**** $phone ** $area_code ** $exchange ** $number ** $exten
+sion ***** ";
}else{print "no match";}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.