• Post Reply Bookmark Topic Watch Topic
  • New Topic

Parsing US Phone Numbers  RSS feed

 
deepak sisodia
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to write a program that will parse a phone number in one of the following formats. It should then display the area code, trunk code, number and extension if available.
In the below example, the program should print the following.

Area code 800
Trunk Code 555
Phone no 1212
Extn 1234

The possible input formats are listed below. The goal is to write the simplest script that accomplish this parsing.

•800-555-1212
•800 555 1212
•800.555.1212
•(800) 555-1212
•1-800-555-1212
•800-555-1212-1234
•800-555-1212x1234
•800-555-1212 ext. 1234
•work 1-(800) 555.1212 #1234


Thank
Deepak Sisodia
 
Sridhar Santhanakrishnan
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Deepak,

Welcome to JR.

JR is NotACodeMill. Please show some initiative. Post whatever you have got so far. In case, you havent started, just google and read up on regular expressions.

Here is something that might help.
 
Rob Spoor
Sheriff
Posts: 21133
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The list of examples on that site looks frighteningly similar to Deepak's list. Deepak, you didn't get that list from the same site, did you? Could you tell us where you got it from? Because something tells me there may be a regex on your source site as well.
 
deepak sisodia
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob and Sree!

Yes you are correct i got this from the same site.... Sorry for being dumb without giving my logic or approach.

I am completely new to Regex. I browsed the API for Pattern and Matches, decided that this will help me to solve this.

public static void main(String[] args) {
String str = "work 1-(800) 555.1212 #1234";
Pattern pattern = Pattern.compile("(\\d{3})");
Matcher matcher = pattern.matcher(str);
System.out.println(matcher.find());
}

but now I am not getting how to add an appropriate pattern and how to get the searched value. (i know we have something liek group()) ..please help me to understand the regix.

Thanks
Deepak
 
James Elsey
Ranch Hand
Posts: 228
Android IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are lots of ways for doing this, but I can't judge as to which is the best method for you.

what about stripping out all the non-digit characters from the string first.

Then you can easier deal with the left over numbers

Such as checking the first digit for the "1". Then working out if an area code is used by checking the length of the number.

Once you know the length of the phone number, it should be pretty easy to slice it up into the international digit, area code, and the phone number

you should be able to do most of the above without regular expressions
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!