为什么下面的正则表达式:$regex = '/\b(V|E)?\d{1,2}? ?\d{3} ?\d{3}\b/i';
不匹配下面的所有输入
我确实认为这(V|E)?\d{1,2}? ?
将使字母、第一个或两个数字和第一个空格成为可选
输入
<?php
$sms = array(
'test test test 11 111 111 test test test',
'test test test 1 111 111 test test test',
'test test test 111 111 test test test', // does not match
'test test test test test test 11111111',
'test test test 1111111 test test test',
'test test test 111111 test test test', // does not match
'test test test E11 111 111 test test test',
'test test test V1 111 111 test test test',
'test test test V111 111 test test test', // does not match
'test test test V11111111 test test test',
'test test test V1111111 test test test',
'test test test E111111 test test test', // does not match
'test test test V 11 111 111 test test test',
'test test test V 1 111 111 test test test',
'test test test E 111 111 test test test', // does not match
'test test test V 11111111 test test test',
'test test test V 1111111 test test test',
'test test test V 111111 test test test', //does not match
'test test test V11 111 111 test test test',
'test test test V1 111 111 test test test',
'test test test E111 111 test test test', //does not match
'test test test V11111111 test test test',
'V1111111 test test test test test test',
'test test test V111111 test test test', // does not match
);
$regex = '/\b(V|E)?\d{1,2}? ?\d{3} ?\d{3}\b/i';
$noMatches = 0;
$index = 0;
foreach($sms as $v) {
$match = preg_match($regex, $v, $matches);
if($match) {
//print_r($matches);
//echo "$v match!\n";
//$matches++;
}
else {
echo "$index - $v does NOT match!\n";
$noMatches++;
}
$index++;
}
$total = count($sms);
echo "\n\nTotal: $total\nNo Matches: $noMatches\n";
输出
$ php test-regex.php
2 - test test test 111 111 test test test does NOT match!
5 - test test test 111111 test test test does NOT match!
8 - test test test V111 111 test test test does NOT match!
11 - test test test E111111 test test test does NOT match!
14 - test test test E 111 111 test test test does NOT match!
17 - test test test V 111111 test test test does NOT match!
20 - test test test E111 111 test test test does NOT match!
23 - test test test V111111 test test test does NOT match!
Total: 24
No Matches: 8
编辑:
使用马里奥建议正则表达式现在是$regex = '/\b(V|E)?\d{0,2} ?\d{3} ?\d{3}\b/i';
,为什么在某些情况下,这个正则表达式不捕获字母V
或E
$output = array(
'test test test E11 111 111 test test test' => 'E11 111 111',
'test test test V1 111 111 test test test' => 'V1 111 111',
'test test test V111 111 test test test' => 'V111 111',
'test test test V11111111 test test test' => 'V11111111',
'test test test V1111111 test test test' => 'V1111111',
'test test test E111111 test test test' => 'E111111',
'test test test V 11 111 111 test test test' => '11 111 111', // Missing Letter
'test test test V 1 111 111 test test test' => '1 111 111', // Missing Leter
'test test test E 111 111 test test test' => 'E 111 111',
'test test test V 11111111 test test test' => '11111111', // Missing Letter
'test test test V 1111111 test test test' => '1111111', // Missing Letter
'test test test V 111111 test test test' => 'V 111111',
'test test test V11 111 111 test test test' => 'V11 111 111',
'test test test V1 111 111 test test test' => 'V1 111 111',
'test test test E111 111 test test test' => 'E111 111',
'test test test V11111111 test test test' => 'V11111111',
'V1111111 test test test test test test' => 'V1111111',
'test test test V111111 test test test' => 'V111111',
'V 1111111 test test test' => '1111111', // Missing Letter
'test test test V 1111111 test test test' => '1111111', // Missing Letter
);