Regex to match all us phone number formats

后端 未结 7 743
借酒劲吻你
借酒劲吻你 2020-12-03 03:32

First of all i would say i have seen many example here and googled but none found that matches all the condition i am looking for some match top 3 not below some inbetween.

相关标签:
7条回答
  • 2020-12-03 04:16

    To add to all of the above suggestions, here's my RegEx that will enforce NANP standards:

    ((?:\(?[2-9](?(?=1)1[02-9]|(?(?=0)0[1-9]|\d{2}))\)?\D{0,3})(?:\(?[2-9](?(?=1)1[02-9]|\d{2})\)?\D{0,3})\d{4})
    

    This regular expression enforces NANP Standard rules such as N11 codes are used to provide three-digit dialing access to special services, and thus excludes them using a conditional capture. It also accounts for up to 3 non-digit characters (\D{0,3}) in between sections, because I've seen some funky data.

    From the provided testing data, here's the Output:

    3087774825
    (281)388-0388
    (281)388-0300
    (979) 778-0978
    (281)934-2479
    (281)934-2447
    (979)826-3273
    (979)826-3255
    (281)356-2530
    (281)356-5264
    (936)825-2081
    (832)595-9500
    (832)595-9501
    281-342-2452
    

    Note that there were two sample values omitted due to not being valid phone numbers by NANP Standards: Area Code begins with 1

    1334714149
    1334431660
    

    The rule I am referring to can be found on the National NANPA's website's Area Codes page stating, The format of an area code is NXX, where N is any digit 2 through 9 and X is any digit 0 through 9.

    0 讨论(0)
提交回复
热议问题