File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Regular Expression for Phone # Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Regular Expression for Phone #" Watch "Regular Expression for Phone #" New topic
Author

Regular Expression for Phone #

Jehan Jaleel
Ranch Hand

Joined: Apr 30, 2002
Posts: 196
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

Joined: Aug 05, 2001
Posts: 2545
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

Joined: Nov 04, 2001
Posts: 1873
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

Joined: May 10, 2001
Posts: 265
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

Joined: Feb 26, 2001
Posts: 5288
    
  10

"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.


Junilu - [How to Ask Questions] [How to Answer Questions]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[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 ]

"I'm not back." - Bill Harding, Twister
John Lee
Ranch Hand

Joined: Aug 05, 2001
Posts: 2545
Thanks Jim!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regular Expression for Phone #