I need a well tested Regular Expression (.net style preferred), or some other simple bit of code that will parse a USA/CA phone number into component parts, so:
- 3035551234122
- 1-303-555-1234x122
- (303)555-1234-122
- 1 (303) 555 -1234-122
etc...
all parse into:
- AreaCode: 303
- Exchange: 555
- Suffix: 1234
- Extension: 122
None of the answers given so far was robust enough for me, so I continued looking for something better, and I found it:
Google's library for dealing with phone numbers
I hope it is also useful for you.
This is the one I use:
I got it from RegexLib I believe.
Strip out anything that's not a digit first. Then all your examples reduce to:
/^1?(\d{3})(\d{3})(\d{4})(\d*)$/
To support all country codes is a little more complicated, but the same general rule applies.
here's a method easier on the eyes provided by the Z Directory (vettrasoft.com), geared towards American phone numbers:
the last line stores the number to database table "phone_number". column values: country_code = "1", area_code = "888", exchange = "872", etc.
Here is a well-written library used with GeoIP for instance:
http://highway.to/geoip/numberparser.inc
This regex works exactly as you want with your examples: