我只想返回与此条件不符的数字。 标准的一个例子是Apt 2,Department 4等
我的代码是我一直在尝试但它不起作用。 结果应该与输出的内容相同 preg_match_all('!\ d!',$ string,$ matches);
我期望一个阵列以2 3 4 4 5 4 5 2 3 2作为输出。它应该排除Apt 92的编号和Block 5中的编号。
$string = "23 St John Apt 92 rer 4, Wellington Country Block 5 No value test 4545 tt 232";
preg_match_all('!\d(^((?:Apartment|Apt|Block|Department|Lot|Number|Villa)\s*)([0-9]+))!', $string, $matches);
var_dump($matches);
答案 0 :(得分:1)
您可以匹配所需内容,然后使用(*SKIP)(?!)
(或(*SKIP)(*F)
或(*SKIP)(*FAIL)
)构造跳过当前匹配的索引:
$re = '/\b(?:Apartment|Apt|Block|Department|Lot|Number|Villa)\s*[0-9]+(*SKIP)(?!)|\d+/';
$str = "23 St John Apt 92 rer 4, Wellington Country Block 5 No value test 4545 tt 232";
preg_match_all($re, $str, $matches);
输出:
Array
(
[0] => 23
[1] => 4
[2] => 4545
[3] => 232
)
答案 1 :(得分:0)
您是否尝试过preg_match_all('/\d+/', $string, $matches);
?