noo*_*der 104 regex pattern-matching
我想为支持以下格式的标准美国型电话号码编写正则表达式:
###-###-####
(###) ###-####
### ### ####
###.###.####
其中#表示任何数字.到目前为止,我想出了以下表达式
^[1-9]\d{2}-\d{3}-\d{4}
^\(\d{3}\)\s\d{3}-\d{4}
^[1-9]\d{2}\s\d{3}\s\d{4}
^[1-9]\d{2}\.\d{3}\.\d{4}
分别.我不太确定最后一个是否适用于虚线检查.我也想知道是否有任何方法可以编写单个表达式而不是4个不同的表达式,以满足我提到的不同格式.如果是这样,我不知道该怎么做.还有我如何修改表达式/表达式,以便我还可以包含一个条件来支持区域代码作为可选组件.就像是
+1 ### ### ####
其中+1是区号,它是可选的.
Rav*_*yal 177
^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$
匹配以下内容
123-456-7890
(123) 456-7890
123 456 7890
123.456.7890
+91 (123) 456-7890
如果您不希望匹配非美国号码使用
^(\+0?1\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$
更新:
 
如下面的用户Simon Weaver所注意到的,如果您还对未格式化数字的匹配感兴趣,请将分隔符字符类设为可选[\s.-]?
^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$
Fra*_*non 109
这个问题可能有很多种变化.这是一个类似于我之前在SO上提出的答案的正则表达式.
^\s*(?:\+?(\d{1,3}))?[-. (]*(\d{3})[-. )]*(\d{3})[-. ]*(\d{4})(?: *x(\d+))?\s*$
它将匹配以下示例以及更多:
18005551234
1 800 555 1234
+1 800 555-1234
+86 800 555 1234
1-800-555-1234
1 (800) 555-1234
(800)555-1234
(800) 555-1234
(800)5551234
800-555-1234
800.555.1234
800 555 1234x5678
8005551234 x5678
1    800    555-1234
1----800----555-1234
无论输入电话号码的方式如何,都可以使用捕获组来分解电话号码,以便您可以在代码中处理它.
如果您有兴趣,请点击表达式:
^\s*                #Line start, match any whitespaces at the beginning if any.
(?:\+?(\d{1,3}))?   #GROUP 1: The country code. Optional.
[-. (]*             #Allow certain non numeric characters that may appear between the Country Code and the Area Code.
(\d{3})             #GROUP 2: The Area Code. Required.
[-. )]*             #Allow certain non numeric characters that may appear between the Area Code and the Exchange number.
(\d{3})             #GROUP 3: The Exchange number. Required.
[-. ]*              #Allow certain non numeric characters that may appear between the Exchange number and the Subscriber number.
(\d{4})             #Group 4: The Subscriber Number. Required.
(?: *x(\d+))?       #Group 5: The Extension number. Optional.
\s*$                #Match any ending whitespaces if any and the end of string.
要使区号可选,只需在(\ d {3})之后为区号添加一个问号.
sto*_*omy 25
^(\+\d{1,2}\s?)?1?\-?\.?\s?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$
1-718-444-1122
718-444-1122
(718)-444-1122
17184441122
7184441122
718.444.1122
1718.444.1122
1-123-456-7890
1 123-456-7890
1 (123) 456-7890
1 123 456 7890
1.123.456.7890
+91 (123) 456-7890
18005551234
1 800 555 1234
+1 800 555-1234
+86 800 555 1234
1-800-555-1234
1 (800) 555-1234
(800)555-1234
(800) 555-1234
(800)5551234
800-555-1234
800.555.1234
18001234567
1 800 123 4567
1-800-123-4567
+18001234567
+1 800 123 4567
+1 (800) 123 4567
1(800)1234567
+1800 1234567
1.8001234567
1.800.123.4567
+1 (800) 123-4567
18001234567
1 800 123 4567
+1 800 123-4567
+86 800 123 4567
1-800-123-4567
1 (800) 123-4567
(800)123-4567
(800) 123-4567
(800)1234567
800-123-4567
800.123.4567
1231231231
123-1231231
123123-1231
123-123 1231
123 123-1231
123-123-1231
(123)123-1231
(123)123 1231
(123) 123-1231
(123) 123 1231
+99 1234567890
+991234567890
(555) 444-6789
555-444-6789
555.444.6789
555 444 6789
18005551234
1 800 555 1234
+1 800 555-1234
+86 800 555 1234
1-800-555-1234
1.800.555.1234
+1.800.555.1234
1 (800) 555-1234
(800)555-1234
(800) 555-1234
(800)5551234
800-555-1234
800.555.1234
(003) 555-1212
(103) 555-1212
(911) 555-1212
18005551234
1 800 555 1234
+86 800-555-1234
1 (800) 555-1234
这是我创建的一个相当紧凑的。
Search: \+?1?\s*\(?-*\.*(\d{3})\)?\.*-*\s*(\d{3})\.*-*\s*(\d{4})$
Replace: +1 \($1\) $2-$3
针对以下用例进行了测试。
18001234567
1 800 123 4567
1-800-123-4567
+18001234567
+1 800 123 4567
+1 (800) 123 4567
1(800)1234567
+1800 1234567
1.8001234567
1.800.123.4567
1--800--123--4567
+1 (800) 123-4567
小智 6
在jsfiddle上使用上述解决方案添加一个示例.我根据客户的要求修改了一些代码.希望这也有助于某人.
/^\s*(?:\+?(\d{1,3}))?[- (]*(\d{3})[- )]*(\d{3})[- ]*(\d{4})(?: *[x/#]{1}(\d+))?\s*$/
小智 5
为巴基斯坦用户试试这个。这是我创建的一个相当紧凑的。
((\+92)|0)[.\- ]?[0-9][.\- ]?[0-9][.\- ]?[0-9]
针对以下用例进行了测试。
+92 -345 -123 -4567
+92 333 123 4567
+92 300 123 4567
+92 321 123 -4567
+92 345 - 540 - 5883
从@Ravi的回答开始,我还对 NPA(区域)代码应用了一些验证规则。
尤其:
还有一些其他限制,包括保留块(N9X、37X、96X)和 555,但我忽略了这些,特别是因为保留块可能会在将来使用,而 555 对于测试很有用。
这就是我想出的:
^((\+\d{1,2}|1)[\s.-]?)?\(?[2-9](?!11)\d{2}\)?[\s.-]?\d{3}[\s.-]?\d{4}$
或者,如果您还想匹配空白值(如果不需要该字段),您可以使用:
(^((\+\d{1,2}|1)[\s.-]?)?\(?[2-9](?!11)\d{2}\)?[\s.-]?\d{3}[\s.-]?\d{4}$|^$)
我对有效数字的测试用例(许多来自@Francis'的回答)是:
18005551234
1 800 555 1234
+1 800 555-1234
+86 800 555 1234
1-800-555-1234
1.800.555.1234
+1.800.555.1234
1 (800) 555-1234
(800)555-1234
(800) 555-1234
(800)5551234
800-555-1234
800.555.1234
我的无效测试用例包括:
(003) 555-1212     // Area code starts with 0
(103) 555-1212     // Area code starts with 1
(911) 555-1212     // Area code ends with 11
180055512345       // Too many digits
1 800 5555 1234    // Prefix code too long
+1 800 555x1234    // Invalid delimiter
+867 800 555 1234  // Country code too long
1-800-555-1234p    // Invalid character
1 (800)  555-1234  // Too many spaces
800x555x1234       // Invalid delimiter
86 800 555 1212    // Non-NA country code doesn't have +
我的正则表达式不包括用于提取数字组的分组,但可以对其进行修改以包括这些数字组。
用于验证常规 10 位电话号码加上可选国际代码(1 到 3 位)和可选分机号码(任意位数)的正则表达式模式:
/(\+\d{1,3}\s?)?((\(\d{3}\)\s?)|(\d{3})(\s|-?))(\d{3}(\s|-?))(\d{4})(\s?(([E|e]xt[:|.|]?)|x|X)(\s?\d+))?/g
演示:https : //www.regextester.com/103299
有效条目:
/* Full number */
+999 (999) 999-9999 Ext. 99999
/* Regular local phone number (XXX) XXX-XXXX */
1231231231
123-1231231
123123-1231
123-123 1231
123 123-1231
123-123-1231
(123)123-1231
(123)123 1231
(123) 123-1231
(123) 123 1231
/* International codes +XXX (XXX) XXX-XXXX */
+99 1234567890
+991234567890
/* Extensions (XXX) XXX-XXXX Ext. XXX... */
1234567890 Ext 1123123
1234567890Ext 1123123
1234567890 Ext1123123
1234567890Ext1123123
1234567890 Ext: 1123123
1234567890Ext: 1123123
1234567890 Ext:1123123
1234567890Ext:1123123
1234567890 Ext. 1123123
1234567890Ext. 1123123
1234567890 Ext.1123123
1234567890Ext.1123123
1234567890 ext 1123123
1234567890ext 1123123
1234567890 ext1123123
1234567890ext1123123
1234567890 ext: 1123123
1234567890ext: 1123123
1234567890 ext:1123123
1234567890ext:1123123
1234567890 X 1123123
1234567890X1123123
1234567890X 1123123
1234567890 X1123123
1234567890 x 1123123
1234567890x1123123
1234567890 x1123123
1234567890x 1123123
Yev*_*r 5
我使用的电话号码正则表达式:
    /^[+]?(\d{1,2})?[\s.-]?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$/
盖子:
我发现这个正则表达式对于 10 位数的联系号码最有用:
^(?:(?:\+|0{0,2})91(\s*[\-]\s*)?|[0]?)?[789]\d{9}$
参考: https: //regex101.com/r/QeQewP/1
解释: