?
Solved

Regex Validation for Phone number format (111)111-1111 in Java

Posted on 2010-01-03
8
Medium Priority
?
2,346 Views
Last Modified: 2013-11-23
Hello,

Please help me with the Regular Expression for the phone number format: (111)111-1111
Language Used: Java

Thanx.
0
Comment
Question by:panshul007
8 Comments
 
LVL 1

Expert Comment

by:Eric_Dahlvang
ID: 26169061
0
 
LVL 3

Expert Comment

by:DooDah
ID: 26169068

\(?\b([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})\b

Phone Number Conversion (North America)
Matches 3334445555, 333.444.5555, 333-444-5555, 333 444 5555, (333) 444 5555 and all combinations thereof.
Replaces all those with (333) 444-5555

0
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 26169081
for (111)111-1111
use this
\([0-9]{3}\)[0-9]{3}-[0-9]{4}
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:panshul007
ID: 26169320
@ gurvinder: i get an error invalid escape sequence.

@ Eric: after using this regex... it is not accepting (111)111-1111 as a valid string.

@DooDah: i dnt want it to accept any format other than  (111)111-1111

Please guys i need urgent help..
0
 
LVL 85

Expert Comment

by:ozo
ID: 26169334
^\\(\\d{3}\\)\\d{3}-\\d{4}$
0
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 26169369
change it to
\([0-9]{3}\)[0-9]{3}\-[0-9]{4}
0
 
LVL 3

Accepted Solution

by:
DooDah earned 2000 total points
ID: 26169409

WHOA!,  did you try it,  It Replaces all those with (333) 444-5555 OR (111)111-1111

>> @DooDah: i dnt want it to accept any format other than  (111)111-1111
\(?\b([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})\b

Phone Number Conversion (North America)
Matches 3334445555, 333.444.5555, 333-444-5555, 333 444 5555, (333) 444 5555 and all combinations thereof.
Replaces all those with (333) 444-5555
0
 

Expert Comment

by:ttthejesterrr
ID: 26179116
I always like to use this handy tool for testing regular expressions:

http://www.javaregex.com/test.html
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month14 days, 18 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question