• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Parsing US Phone Numbers

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 231
Android IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic