我的目标是尽可能匹配国内和国际电话号码,除了一个测试输入上有一个前导空格外,我的模式非常有效。还在寻找其他我遗漏的示例数据的常规改进。
我不太想知道电话的格式是否符合该国家/地区的格式,而更多的是(例如:严格来说不是美国)。
模式如下,并且可以在此处测试数据内容:https://regex101.com/r/baJxxw/1
\+?1?[\ \-\.]?\(?\d{2,4}\)?([\ \-\.]?\d+)+(\ ?[xX]\d+)?
我尝试匹配的输入(贪婪),但由于635-48018
的预期结果而失败,但得到635-48018
是:
(089) / 636-48018 Domestic <-- fails (matches with leading space)
完整的数据示例在这里(任何实际数字都是偶然的):
333-555-1075
333.555.1075
(333)555.1075
(333)555-1075
(333)555-1075 x4141
(333) 555-1075
(333) 555-1075 x4141
1 (333) 555-1075
1 (333) 555-1075 x4141
1-333-555-1075
1-333-555-1075 X4141
1.800.591.5911
1.800.591.5911 x41
1.800.591.5911X414166
1.800.591.5911 X4141
+1555789123
+4974339296
+591 74339296
+1 555 555 5554
+1 555 555 5554 x4141
+(591) 7433433
+(591) (4) 6434850 <--fail, but OK
0591 74339296
0001 5555555555
(0001) 5555555
59145678464
645-00-0000 <-- also matches social security number, but OK for my needs
555-3010 Local
(541) 555-3010 Domestic
+1-541-555-3010 International
1-541-555-3010 Dialed in the US
001-541-555-3010 Dialed from Germany
191 541 555 3010 Dialed from France
636-48018 Local (Germany telephone)
(089) / 636-48018 Domestic <-- fails (matches with leading space)
089/636-48018 Domestic <-- fails
+49-89-636-48018 International
19-49-89-636-48018 Dialed from France
所需的变量是PRCE(php)和ECMAScript。