What is the best regular expression for phone numbers? [duplicate]

杀马特。学长 韩版系。学妹 提交于 2019-12-05 22:42:14

I'd go with

# countryCode
(?:\+\d{1,3}|0\d{1,3}|00\d{1,2})

# actual number
(?:[-\/\s.]|\d)+

# combined with optional country code and parantheses in between
 ^(?:\+\d{1,3}|0\d{1,3}|00\d{1,2})?(?:\s?\(\d+\))?(?:[-\/\s.]|\d)+$

This should be good enough to match most most European and US representations of a phone Number as

+49 123 999
0001 123.456
+31 (0) 8123

But in general there are too many overlapping textual representation of a phone Number as to use a single Regex for different countries. If you could match all representations you'll get to many false positives. As what may be a number in Switzerland may be something else in Russia, Germany or Ghana. It's all about balancing precission and recall and optimizing the Regex to the countries you really need.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!