This regular expression will parse most phone numbers into four groups: country code, area code, number, and extension. It works most of the time, see the examples below.
Here’s the expression:
1
| |
Again, as a Java string:
1
| |
Results:
| Target String | matches() | group(0) | group(1) | group(2) | group(3) | group(4) |
|---|---|---|---|---|---|---|
| 011-656-555-1234 | Yes | 011-656-555-1234 | 011 | 656 | 555-1234 | |
| (217)555-1234 | Yes | (217)555-1234 | 217 | 555-1234 | ||
| (907) 555-1234 | Yes | (907) 555-1234 | 907 | 555-1234 | ||
| +82-10-5551-2345 | Yes | +82-10-5551-2345 | 82 | 10 | 5551-2345 | |
| +(82) 10-5551-2345 | Yes | +(82) 10-5551-2345 | 82 | 10 | 5551-2345 | |
| +886-2-55512345 | Yes | +886-2-55512345 | 886 | 2 | 55512345 | |
| 1-416-555-1234 ext 1234 | Yes | 1-416-555-1234 ext 1234 | 1 | 416 | 555-1234 | 1234 |
| 714 555 1234 / 1234 | Yes | 714 555 1234 / 1234 | 714 | 555 1234 | 1234 | |
| +1-714-555-1234 | Yes | +1-714-555-1234 | 1 | 714 | 555-1234 | |
| +(82) 10-5326-5760 | Yes | +(82) 10-5326-5760 | 82 | 10 | 5326-5760 |