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

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?

其他回答

我在另一个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”和前导的“+”符号),注意,因为英国人倾向于用非标准形式的+44(0)写数字……当要求使用国际前缀时(在这种特定情况下,应该完全丢弃(0))。

然后,你会得到这样的值:

 12345678901
 12345678901x1234
 345678901x1234
 12344678901
 12345678901
 12345678901
 12345678901
 +4112345678
 +441234567890

然后,当你显示,重新格式化为您的心脏内容。如。

  1 (234) 567-8901
  1 (234) 567-8901 x1234

这里有一个非常棒的模式,它最接近我需要实现的验证。我不是原作者,但我认为这很值得分享,因为我发现这个问题非常复杂,没有一个简明或广泛有用的答案。

下面的正则表达式将捕获各种全球电话号码格式中广泛使用的数字和字符组合:

/ ^ \ s * (?: \ + ? (\ d{1,3})) ?([-。() * (\ d{3})(-)。) *) ? ((\ d{3})[-。) * (\ d{2,4})(?:[-。x] * (\ d +)) ?) \ s *美元/通用

正面: + 42 555.123.4567 -123-4567 + 1 - (800) 电话:+7 555 1234567 + 7 (926 1234567) (926 1234567) + 79261234567 926 1234567 9261234567 1234567 123-4567 123-89-01 495 1234567 469 123 45 67 89261234567 8 (926) 1234567 926.123.4567 415-555-1234 650-555-2345 (416) 555-3456 202 555 4567 4035555678 1 416 555 9292

负面: 926 3 4 8 800 600个苹果

原始来源:http://www.regexr.com/38pvb

请注意,剥离()字符不适用于常见的英国号码的书写风格:+44(0)1234 567890,这意味着可以拨打国际号码: + 441234567890 或在英国拨打01234567890

由于这篇文章没有语言标签,我将给出一个在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