im正在尝试验证php中的电话号码。要求为(0d)dddddd或0d dddddd,其中d为0-9。这就是我现在拥有的
if(!preg_match("/^0[0-9]{9}$/", $phone))
{
//wrong format
}
我试过几个类似的问题,但仍然不能很好地理解regex。有人能帮我修复正则表达式吗?
您可以尝试以下代码,
if(!preg_match("~^(?:0'd's|'(0'd'))'d{8}$~", $phone))
{
//wrong format
}
演示
解释:
^ the beginning of the string
(?: group, but do not capture:
0 '0'
'd digits (0-9)
's whitespace
| OR
'( '('
0 '0'
'd digits (0-9)
') ')'
) end of grouping
'd{8} digits (0-9) (8 times)
$ before an optional 'n, and the end of the
string
^(?:'(0'd')|0'd's)'d{8}$
试试这个。请参阅演示。
http://regex101.com/r/wQ1oW3/8
^(('(0'd'))|(0'd ))'d{8}$
匹配
(05)12345678
05 12345678
参见示例http://regex101.com/r/zN4jE4/1
if(!preg_match("/^(('(0'd'))|(0'd ))'d{8}$/", $phone))
{
//wrong format
}
尝试这个
if(!preg_match("^(?:'(0'd')|0'd's)'d{8}$", $phone))
{
//wrong format
}