Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regular Expression for Phone #

 
Jehan Jaleel
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone give me a Regular Expression that matches with domestic phone #. The patter can be simple: 3 numbers followed by a space or ) followed by 3 numbers followed by a space or - followed by 4 numbers.
Thanks in advance for any help,
Jehan
 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by JehanNYNJ:
Can someone give me a Regular Expression that matches with domestic phone #. The patter can be simple: 3 numbers followed by a space or ) followed by 3 numbers followed by a space or - followed by 4 numbers.
Thanks in advance for any help,
Jehan

I think it is:
(xxx) xxx-xxxx.
I could be wrong.
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
don't we have to follow the international phone number convention that might work for most places?
like,
(country code)-(city/state code)-(local phone format)
now the third part "local phone format" would be specific to the place we live in.
e.g.
US: 1-323-111-1111. i guess the local number is 111-1111 format in US , right?
India:91-79-1111111
91 = india STD code
79 = ahmedabad (city in gujarat state) STD code
rest is the actual phone number
i don't know if you really want to build generic reg. exp that would work for every place on the earth...just my 2 cents...
thanks!
maulin.
 
Chad McGowan
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the Pattern documentation here
I think you will find what you are looking for.
[ January 24, 2003: Message edited by: Chad McGowan ]
 
Junilu Lacar
Bartender
Pie
Posts: 7758
61
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"JehanNYNJ"
Do a search on "phone" at http://www.regexlib.com
BTW, please change your publicly displayed name to conform with the Official JavaRanch Naming Policy. You can do that here Thanks for your cooperation.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[This was composed earlier, but I was interrupted before posting - so this ignores other references to regex docs and libraries]
Maulin - you're correct, an international format would be much more complex. But Jehan did say "domestic" and specified the particular format to be used, so as long as he knows it won't be valid for international numbers, fine.
Don - I assume when Jehan asks for a regular expression, he means one in the format used by java.util.regex.Pattern or various other languages like Perl. Your answer won't work here - "x" means "x", not "any digit", and unescaped parentheses denote a capturing group. A better answer would be something like:
[(??:\(\d{3}\)[ -]?|\d{3}[ -]))?\d{3}[ -]\d{4}]
(Convert every \ to \\ to express this as a Java String literal.)
This behaves a little differently than the original spec, allowing a few more "common sense" variations, and requiring a matching pair of parentheses if parens are used at all. Here's a test program showing how it behaves:

[ January 24, 2003: Message edited by: Jim Yingst ]
 
John Lee
Ranch Hand
Posts: 2545
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jim!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic