Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 匹配字符串中的精确数字(在较大的数字中没有部分匹配)_Php_Regex - Fatal编程技术网

Php 匹配字符串中的精确数字(在较大的数字中没有部分匹配)

Php 匹配字符串中的精确数字(在较大的数字中没有部分匹配),php,regex,Php,Regex,我想匹配字符串中的精确数字。例如,我搜索“123”并希望在“123”、“asdf123x”、“99 123”中匹配它,但如果它只是“较大”数字中的部分匹配,则不匹配。因此它将不匹配“0123”、“1234”、“123123” 因为“asdf123x”,我不能使用单词边界。 我曾经尝试过这样一个消极的前瞻(并计划在后面添加一个消极的前瞻,但即使是前瞻本身也不能像我想象的那样起作用: $string = "123"; //or one of the other examples preg_ma

我想匹配字符串中的精确数字。例如,我搜索
“123”
并希望在
“123”、“asdf123x”、“99 123”
中匹配它,但如果它只是“较大”数字中的部分匹配,则不匹配。因此它将不匹配
“0123”、“1234”、“123123”

因为“asdf123x”,我不能使用单词边界。 我曾经尝试过这样一个消极的前瞻(并计划在后面添加一个消极的前瞻,但即使是前瞻本身也不能像我想象的那样起作用:

$string = "123"; //or one of the other examples   
preg_match('/(?!\d)123/',$string,$matches);

这永远不会匹配,我也不明白为什么。

你需要消极的回顾和前瞻:

'/(?<!\d)123(?!\d)/'
  ^^^^^^^   ^^^^^^

天哪,我错了解释单词“lookback”和“lookahead”,因此我尝试了“/(?!\d)123(?实际上,
(?!\d)123
将永远不会匹配,因为
(?!\d)
要求下一个字符不是数字。是的,重点是我使用了我想要匹配的模式的lookahead“ahead”-错解释“lookeahead”
$string = "123"; //or one of the other examples   
if (preg_match("/(?<!\d)$string(?!\d)/", "123123",$matches)) {
    echo "Matched!";
} else {
    echo "Not matched!";
}