gpt4 book ai didi

php - 北美编号计划正则表达式

转载 作者:行者123 更新时间:2023-12-01 22:38:48 28 4
gpt4 key购买 nike

验证 NAPN 的正则表达式是否正常?数字?没有可能的“优化”?

^\+1[2-9](0(?!0)|1(?!1)|2(?!2)|3(?!3)|4(?!4)|5(?!5)|6(?!6)|7(?!7)|8(?!8)|9(?!9))[0-9][2-9]((0|[2-9]){2}|1(?!1)[0-9]|(0|[2-9])1)[0-9]{4}$

最佳答案

您的链接包含匹配信息:

NPA (Numbering Plan Area Code) - Allowed ranges: [2–9] for the first digit, and [0-9] for the second and third digits. When the second and third digits of an area code are the same, that code is called an easily recognizable code (ERC). ERCs designate special services; e.g., 888 for toll-free service. The NANP is not assigning area codes with 9 as the second digit.

NXX (Central Office) - Allowed ranges: [2–9] for the first digit, and [0–9] for both the second and third digits (however, the third digit cannot be "1" if the second digit is also "1").

xxxx (Subscriber Number) -[0–9] for each of the four digits.

如果你想省略开头的+1,那么你可以使用下面的来匹配10位数字。他们唯一要阻止的是包含一些 3 位本地代码的号码,例如 911611411 等。所以为使这项工作正常进行,我们要确保号码的中心办公室部分中第一位之后的接下来的 2 位数字不是 (?!11) 具有负前瞻性的数字。

此模式适用于大多数字符串。

$pattern = '~^\(?([2-9][0-9]{2})\)?[-. ]?([2-9](?!11)[0-9]{2})[-. ]?([0-9]{4})$~';

$numbers = array(
'(800) 555 1212',
'(800) 911 1212',
'(800) 910 1212',
'(800) 901 1212',
'(100) 455 1212',
'(800) 155 1212',
'555 555 1212',
'813.555.1212',
);



foreach($numbers as $number){
if(preg_match($pattern,$number)){
echo "$number is valid.\n";
} else {
echo "$number is invalid. \n";
}
}

输出

(800) 555 1212 is valid.
(800) 911 1212 is invalid.
(800) 910 1212 is valid.
(800) 901 1212 is valid.
(100) 455 1212 is invalid.
(800) 155 1212 is invalid.
555 555 1212 is valid.
813.555.1212 is valid.

关于php - 北美编号计划正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18385827/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com