我试图把一个全面的正则表达式放在一起来验证电话号码。理想情况下,它可以处理国际格式,但它必须处理美国格式,包括以下内容:
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
我会用我目前的尝试来回答,但我希望有人有更好的和/或更优雅的东西。
我试图把一个全面的正则表达式放在一起来验证电话号码。理想情况下,它可以处理国际格式,但它必须处理美国格式,包括以下内容:
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
我会用我目前的尝试来回答,但我希望有人有更好的和/或更优雅的东西。
当前回答
我写得最简单(虽然我不需要点)。
^([0-9\(\)\/\+ \-]*)$
正如下面提到的,它只检查字符,而不检查其结构/顺序
其他回答
我在另一个SO问题上回答了这个问题,然后决定把我的答案也包括在这个线程上,因为没有人解决如何要求/不要求项目,只是分发正则表达式: 正则表达式工作错误,匹配意想不到的东西
从我在那个网站上的帖子中,我创建了一个快速指南,帮助任何人为自己想要的电话号码格式制作自己的正则表达式,我要警告(就像我在另一个网站上所做的那样),如果你限制太多,你可能不会得到想要的结果,并且没有“一宜之计”的解决方案来接受世界上所有可能的电话号码——只有你决定接受的格式作为你选择的格式。使用风险自负。
快速小抄
Start the expression: /^ If you want to require a space, use: [\s] or \s If you want to require parenthesis, use: [(] and [)] . Using \( and \) is ugly and can make things confusing. If you want anything to be optional, put a ? after it If you want a hyphen, just type - or [-] . If you do not put it first or last in a series of other characters, though, you may need to escape it: \- If you want to accept different choices in a slot, put brackets around the options: [-.\s] will require a hyphen, period, or space. A question mark after the last bracket will make all of those optional for that slot. \d{3} : Requires a 3-digit number: 000-999. Shorthand for [0-9][0-9][0-9]. [2-9] : Requires a digit 2-9 for that slot. (\+|1\s)? : Accept a "plus" or a 1 and a space (pipe character, |, is "or"), and make it optional. The "plus" sign must be escaped. If you want specific numbers to match a slot, enter them: [246] will require a 2, 4, or 6. (?:77|78) or [77|78] will require 77 or 78. $/ : End the expression
我不建议使用正则表达式。
和上面的答案一样,去掉所有难看的电话号码,这样你就只剩下一串数字字符,如果提供扩展名的话,还会有一个'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
这涵盖了相当多的内容。它不是一个正则表达式,但它确实很容易映射到其他语言。
我相信Number::Phone::US和Regexp::Common(尤其是Regexp::Common::URI::RFC2806的源代码)Perl模块会有所帮助。
应该更详细地说明这个问题,以解释验证这些数字的目的。例如,911在美国是一个有效的号码,但911x不是x的任何值,这样电话公司就可以计算出你什么时候拨号。关于这个问题有几种不同的说法。但是您的正则表达式不检查区域代码部分,因此这似乎不是一个问题。
就像验证电子邮件地址一样,即使你有一个有效的结果,你也无法知道它是否分配给了某人,直到你尝试它。
如果您正在尝试验证用户输入,为什么不规范化结果并处理它呢?如果用户输入的数字您不能识别为有效数字,则将其保存为输入值或删除不可用字符。Number::Phone::Normalize Perl模块可能是灵感的来源。
Java为有效的电话号码生成REGEX
另一种替代方法是让Java生成一个REGEX,该REGEX处理从列表中读取的所有电话号码变体。这意味着名为validPhoneNumbersFormat的列表(如下面的代码上下文所示)决定哪种电话号码格式是有效的。
注意:这种类型的算法适用于任何处理正则表达式的语言。
生成REGEX的代码片段:
Set<String> regexSet = uniqueValidPhoneNumbersFormats.stream()
.map(s -> s.replaceAll("\\+", "\\\\+"))
.map(s -> s.replaceAll("\\d", "\\\\d"))
.map(s -> s.replaceAll("\\.", "\\\\."))
.map(s -> s.replaceAll("([\\(\\)])", "\\\\$1"))
.collect(Collectors.toSet());
String regex = String.join("|", regexSet);
上下文中的代码片段:
public class TestBench {
public static void main(String[] args) {
List<String> validPhoneNumbersFormat = Arrays.asList(
"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",
"+12345678901",
"(234) 567-8901 ext. 123",
"+1 234-567-8901 ext. 123",
"1 (234) 567-8901 ext. 123",
"00 1 234-567-8901 ext. 123",
"+210-998-234-01234",
"210-998-234-01234",
"+21099823401234",
"+210-(998)-(234)-(01234)",
"(+351) 282 43 50 50",
"90191919908",
"555-8909",
"001 6867684",
"001 6867684x1",
"1 (234) 567-8901",
"1-234-567-8901 x1234",
"1-234-567-8901 ext1234",
"1-234 567.89/01 ext.1234",
"1(234)5678901x1234",
"(123)8575973",
"(0055)(123)8575973"
);
Set<String> uniqueValidPhoneNumbersFormats = new LinkedHashSet<>(validPhoneNumbersFormat);
List<String> invalidPhoneNumbers = Arrays.asList(
"+210-99A-234-01234", // FAIL
"+210-999-234-0\"\"234", // FAIL
"+210-999-234-02;4", // FAIL
"-210+998-234-01234", // FAIL
"+210-998)-(234-(01234" // FAIL
);
List<String> invalidAndValidPhoneNumbers = new ArrayList<>();
invalidAndValidPhoneNumbers.addAll(invalidPhoneNumbers);
invalidAndValidPhoneNumbers.addAll(uniqueValidPhoneNumbersFormats);
Set<String> regexSet = uniqueValidPhoneNumbersFormats.stream()
.map(s -> s.replaceAll("\\+", "\\\\+"))
.map(s -> s.replaceAll("\\d", "\\\\d"))
.map(s -> s.replaceAll("\\.", "\\\\."))
.map(s -> s.replaceAll("([\\(\\)])", "\\\\$1"))
.collect(Collectors.toSet());
String regex = String.join("|", regexSet);
List<String> result = new ArrayList<>();
Pattern pattern = Pattern.compile(regex);
for (String phoneNumber : invalidAndValidPhoneNumbers) {
Matcher matcher = pattern.matcher(phoneNumber);
if(matcher.matches()) {
result.add(matcher.group());
}
}
// Output:
if(uniqueValidPhoneNumbersFormats.size() == result.size()) {
System.out.println("All valid numbers was matched!\n");
}
result.forEach(System.out::println);
}
}
输出:
All valid numbers was matched!
1-234-567-8901
1-234-567-8901 x1234
1-234-567-8901 ext1234
...
...
...
我还建议查看“libphonenumber”谷歌库。我知道它不是正则表达式,但它确实是你想要的。
例如,它会识别:
15555555555
是可能的数字,但不是有效数字。它还支持美国以外的国家。
功能亮点:
Parsing/formatting/validating phone numbers for all countries/regions of the world. getNumberType - gets the type of the number based on the number itself; able to distinguish Fixed-line, Mobile, Toll-free, Premium Rate, Shared Cost, VoIP and Personal Numbers (whenever feasible). isNumberMatch - gets a confidence level on whether two numbers could be the same. getExampleNumber/getExampleNumberByType - provides valid example numbers for all countries/regions, with the option of specifying which type of example phone number is needed. isPossibleNumber - quickly guessing whether a number is a possible phonenumber by using only the length information, much faster than a full validation. isValidNumber - full validation of a phone number for a region using length and prefix information. AsYouTypeFormatter - formats phone numbers on-the-fly when users enter each digit. findNumbers - finds numbers in text input. PhoneNumberOfflineGeocoder - provides geographical information related to a phone number.
例子
电话号码验证最大的问题是它非常依赖文化。
美国 (408) 974-2042是有效的美国号码 (999) 974-2042不是有效的美国号码 澳大利亚 0404 999 999是一个有效的澳大利亚号码 (02) 9999 9999也是一个有效的澳大利亚号码 (09) 9999 9999不是有效的澳大利亚号码
正则表达式可以用于检查电话号码的格式,但它不能真正地检查电话号码的有效性。
我建议跳过简单的正则表达式来测试你的电话号码,并使用一个库,如谷歌的libphonenumber(链接到GitHub项目)。
引入libphonenumber !
使用一个更复杂的示例,1-234-567-8901 x1234,您从libphonenumber(链接到在线演示)中获得以下数据:
Validation Results
Result from isPossibleNumber() true
Result from isValidNumber() true
Formatting Results:
E164 format +12345678901
Original format (234) 567-8901 ext. 123
National format (234) 567-8901 ext. 123
International format +1 234-567-8901 ext. 123
Out-of-country format from US 1 (234) 567-8901 ext. 123
Out-of-country format from CH 00 1 234-567-8901 ext. 123
因此,您不仅可以了解电话号码是否有效(它确实有效),而且还可以在您的地区获得一致的电话号码格式。
作为额外的奖励,libphonenumber有许多数据集来检查电话号码的有效性,因此检查诸如+61299999999((02)99999999的国际版本)这样的号码,返回一个带有格式的有效号码:
Validation Results
Result from isPossibleNumber() true
Result from isValidNumber() true
Formatting Results
E164 format +61299999999
Original format 61 2 9999 9999
National format (02) 9999 9999
International format +61 2 9999 9999
Out-of-country format from US 011 61 2 9999 9999
Out-of-country format from CH 00 61 2 9999 9999
Libphonenumber还为您提供了许多额外的好处,例如获取检测到的电话号码所在的位置,以及从电话号码中获得时区信息:
PhoneNumberOfflineGeocoder Results
Location Australia
PhoneNumberToTimeZonesMapper Results
Time zone(s) [Australia/Sydney]
但是无效的澳大利亚电话号码((09)99999999)返回它不是一个有效的电话号码。
Validation Results
Result from isPossibleNumber() true
Result from isValidNumber() false
谷歌的版本有Java和Javascript的代码,但人们也实现了使用谷歌i18n电话号码数据集的其他语言的库:
PHP: https://github.com/giggsey/libphonenumber-for-php Python: https://github.com/daviddrysdale/python-phonenumbers Ruby: https://github.com/sstephenson/global_phone c#: https://github.com/twcclegg/libphonenumber-csharp objective - c: https://github.com/iziz/libPhoneNumber-iOS JavaScript: https://github.com/ruimarinho/google-libphonenumber 灵丹妙药:https://github.com/socialpaymentsbv/ex_phone_number
除非您确定始终接受来自一个地区的数字,并且始终采用同一种格式,否则我强烈建议不要为此编写自己的代码,并使用libphonenumber来验证和显示电话号码。