我试图把一个全面的正则表达式放在一起来验证电话号码。理想情况下,它可以处理国际格式,但它必须处理美国格式,包括以下内容:

1-234-567-8901 1-234-567-8901 x1234 1-234-567-8901 ext1234 1 (234) 567-8901 1.234.567.8901 1/234/567/8901 12345678901

我会用我目前的尝试来回答,但我希望有人有更好的和/或更优雅的东西。


当前回答

我倾向于同意去掉非数字,只接受最好的。也许是为了确保至少有几个数字,尽管这确实禁止像字母电话号码“ASK-JAKE”这样的东西。

一些简单的perl表达式可能是:

@f = /(\d+)/g;
tr/0-9//dc;

使用第一个来保持数字组在一起,这可能会提供格式化线索。使用第二个来简单地丢弃所有非数字。

您是否担心可能需要暂停,然后输入更多的键?或者是555-1212(等哔声)123?

其他回答

由于这篇文章没有语言标签,我将给出一个在python中使用的正则表达式解决方案。

表达式本身:

1[\s./-]?\(?[\d]+\)?[\s./-]?[\d]+[-/.]?[\d]+\s?[\d]+

在python中使用时:

import re

phonelist ="1-234-567-8901,1-234-567-8901 1234,1-234-567-8901 1234,1 (234) 567-8901,1.234.567.8901,1/234/567/8901,12345678901"

phonenumber = '\n'.join([phone for phone in re.findall(r'1[\s./-]?\(?[\d]+\)?[\s./-]?[\d]+[-/.]?[\d]+\s?[\d]+' ,phonelist)])
print(phonenumber)

输出:

1-234-567-8901
1-234-567-8901 1234
1-234-567-8901 1234
1 (234) 567-8901
1.234.567.8901
1/234/567/8901
12345678901

我不建议使用正则表达式。

和上面的答案一样,去掉所有难看的电话号码,这样你就只剩下一串数字字符,如果提供扩展名的话,还会有一个'x'。

在Python中:

注意:BAD_AREA_CODES来自一个文本文件,您可以从web上获取。

BAD_AREA_CODES = open('badareacodes.txt', 'r').read().split('\n')

def is_valid_phone(phone_number, country_code='US'):
    """for now, only US codes are handled"""
    if country_code:
        country_code = country_code.upper()

    #drop everything except 0-9 and 'x'
    phone_number = filter(lambda n: n.isdigit() or n == 'x', phone_number)

    ext = None
    check_ext = phone_number.split('x')
    if len(check_ext) > 1:
        #there's an extension. Check for errors.
        if len(check_ext) > 2:
            return False
        phone_number, ext = check_ext

    #we only accept 10 digit phone numbers.
    if len(phone_number) == 11 and phone_number[0] == '1':
        #international code
        phone_number = phone_number[1:]
    if len(phone_number) != 10:
        return False

    #area_code: XXXxxxxxxx 
    #head:      xxxXXXxxxx
    #tail:      xxxxxxXXXX
    area_code = phone_number[ :3]
    head      = phone_number[3:6]
    tail      = phone_number[6: ]

    if area_code in BAD_AREA_CODES:
        return False
    if head[0] == '1':
        return False
    if head[1:] == '11':
        return False

    #any other ideas?
    return True

这涵盖了相当多的内容。它不是一个正则表达式,但它确实很容易映射到其他语言。

虽然它不是正则表达式,但您可以使用Python库DataPrep中的validate_phone()函数来验证美国电话号码。使用pip Install dataprep安装它。

>>> from dataprep.clean import validate_phone
>>> df = pd.DataFrame({'phone': ['1-234-567-8901', '1-234-567-8901 x1234', 
         '1-234-567-8901 ext1234', '1 (234) 567-8901', '1.234.567.8901',
         '1/234/567/8901', 12345678901, '12345678', '123-456-78987']})
>>> validate_phone(df['phone'])
0     True
1     True
2     True
3     True
4     True
5     True
6     True
7    False
8    False
Name: phone, dtype: bool

你可能会更好地使用掩码输入。这样用户只能输入数字,你可以设置你认为合适的格式。我不确定这是否是一个web应用程序,但如果是有一个非常点击jQuery插件,提供了一些选项来做这件事。

http://digitalbush.com/projects/masked-input-plugin/

他们甚至在教程中讨论了如何屏蔽电话号码输入。

执行替换格式化字符,然后检查剩余的电话有效性。在PHP中,

 $replace = array( ' ', '-', '/', '(', ')', ',', '.' ); //etc; as needed
 preg_match( '/1?[0-9]{10}((ext|x)[0-9]{1,4})?/i', str_replace( $replace, '', $phone_num );

打破像这样复杂的regexp同样有效,但要简单得多。