简体   繁体   English

具有特定区号和格式的电话号码的正则表达式

[英]Regex for phone number with specific area code and format

I'm trying to create a regular expression that would validate a specific phone number with an 868 area code and particular format h thus I want these 我正在尝试创建一个正则表达式来验证具有868区号和特定格式h的特定电话号码因此我想要这些

8680000000

(868) 000 0000

868-000-0000

868 000-0000

to pass validation but these 通过验证,但这些

8880000000

767-000-0000

868-0000000

(868)0000000

to fail validation. 验证失败。

So far I got: 到目前为止我得到了:

^((\(){0,1}(868){1}(\)){0,1})+:?((((\ |\-)(\d{3})(\ |\-)):?\d{4})+|(\d{4,7}))

But

(868)0000000 

is validating 正在验证

I genuinely do not understand why everyone need to validate a phone number with strict spacing, bracketing, hyphenation, and god-knows-what-else requirements. 我真的不明白为什么每个人都需要通过严格的间距,包围,连字和神知道的其他要求验证电话号码。

Strip out all unnecessary characters, validate what needs validating, move on with life. 删除所有不必要的字符,验证需要验证的内容,继续生活。

$phone = '(868) 555-1234'; // or 868 555.1234 or +868x55-5-123-4 etc
$phone = preg_replace('/[^0-9]/', '', $phone); // 8685551234
if( substring($phone, 0, 3) == '868' ) {
  // great success
}

Bonus round. 奖金回合。 Format your data for hu-man display: 为hu-man显示格式化数据:

$phone_display = sprintf('(%s) %s-%s',
  substring($phone,0,3),
  substring($phone,3,3),
  substring($phone,6,4));

Actually this is somewhat hard-coded because you are having some specific requirements like validating 868-000-0000 but not 868-0000000 . 实际上这有点硬编码,因为您有一些特定的要求,如验证868-000-0000但不是868-0000000

Regex is: 正则表达式是:

(^\(868\)\ \d{3}\ \d{4})|(^868\d{7})|(^868\-\d{3}\-\d{4})|(^868\ \d{3}\-\d{4})

DEMO DEMO

Such regex 这样的正则表达式

^(\(?868\)?(\s*|-)\d{3}(\s|-)\d{4}|868\d{7})$

Validate these 验证这些

8680000000
(868) 000 0000
868-000-0000
868 000-0000

Not validate 不验证

8880000000
767-000-0000
868-0000000
(868)0000000

match[1] contains number match[1]包含数字

DEMO and explanation of the regex DEMO和正则表达式的解释

You can do this with some conditionals 你可以用一些条件来做到这一点

 # ^(\()?868(?(1)\)[ ]\d{3}[ ]\d{4}|(?:(?:(-)|([ ])|)\d{3}(?(2)\2|(?(3)[ -]))\d{4}))$

 ^                             # BOS
 ( \( )?                       # (1), Form (xxx) xxx xxxx
 868                           # 868 Area Code
 (?(1)                         # Need Form 1 ?
      \)
      [ ] \d{3} [ ] \d{4} 
   |                              # else
      (?:
           (?:
                ( - )                         # (2), Form xxx-xxx-xxxx
             |  ( [ ] )                       # (3), Form xxx xxx[ -]xxxx
             |                              # Form xxxxxxxxxx
           )
           \d{3}      
           (?(2)                         # Need form 2 ?
                \2 
             |                              # else
                (?(3) [ -] )                  # Need form 3 ?
           )
           \d{4}      
      )
 )
 $                             # EOS

Passed: 通过:

8680000000 86.8亿
(868) 000 0000 (868)000 0000
868-000-0000 868-000-0000
868 000-0000 868 000-0000

Failed: 失败:

8880000000 88.8亿
767-000-0000 767-000-0000
868-0000000 868-0000000
(868)0000000 (868)0000000

You could write one regular expression that matches all the formats, but there is no rule that says you have to. 您可以编写一个匹配所有格式的正则表达式,但没有规则表明您必须这样做。

Matching each format separately makes your code quite a bit more readable: 分别匹配每种格式使您的代码更具可读性:

function matchPhoneNumber($phoneNumber) {
    $regex = [
        '/^(?P<prefix>868)(?P<first>\d{3})(?P<second>\d{4})$/',
        '/^(?P<prefix>868)-(?P<first>\d{3})-(?P<second>\d{4})$/',
        '/^(?P<prefix>868)\s(?P<first>\d{3})-(?P<second>\d{4})$/',
        '/^\((?P<prefix>868)\)\s(?P<first>\d{3})\s(?P<second>\d{4})$/',
    ];
    foreach ($regex as $reg) {
        if (preg_match($reg, $phoneNumber, $match) === 1) {
            return $match;
        }
    }
    return false;
}

Assuming PHP uses EREs like awk or grep -E does: 假设PHP使用像awkgrep -E这样的ERE:

$ grep -E '^((868([0-9]{3}|[- ][0-9]{3}-))|(\(868\) [0-9]{3} ))[0-9]{4}$' file
8680000000
(868) 000 0000
868-000-0000
868 000-0000

or if you like \\d instead of [0-9] : 或者如果你喜欢\\d而不是[0-9]

^((868(\d{3}|[- ]\d{3}-))|(\(868\) \d{3} ))\d{4}$

Anything briefer would match other phone number formats that you didn't specify as being valid and so I'm assuming are invalid. 任何简报都会匹配您未指定为有效的其他电话号码格式,因此我假设无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM