Fork me on GitHub

bill-ingram.com

Programming | Web Design | Photography

Regular Expression for Parsing Phone Numbers

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
^(?:[\+]?[\(]?([\d]{1,3})[\s\-\.\)]+)?(?:[\(]?([\d]{1,3})[\s\-\/\)]+)([2-9][0-9\s\-\.]{6,}[0-9])(?:[\s\D]+([\d]{1,5}))?$

Again, as a Java string:

1
"^(?:[\\+]?[\\(]?([\\d]{1,3})[\\s\\-\\.\\)]+)?(?:[\\(]?([\\d]{1,3})[\\s\\-\\/\\)]+)([2-9][0-9\\s\\-\\.]{6,}[0-9])(?:[\\s\\D]+([\\d]{1,5}))?$"

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

Comments