php正则预查

时间:2023-03-09 20:54:58
php正则预查

php正则预查

// 把ing结尾的单词词根部分(即不含ing部分)找出来
$str = 'hello ,when i am working , don not coming';
零宽度:站在原地往前看,不消耗字符,叫零宽度
前瞻:往前看

断言:判断可能会是什么
正预测:判断是否是准确的ing或者规定的字符
//$patt = '/\b(\w+)ing\b/';//前边的不管,后面的ing拿出来
//$patt = '/\b\w+(?=ing)\b/';//语义矛盾,没有谁后面是ing,同时又是\b
$patt = '\b\w+(?=ing\b)/';
preg_match_all($patt, $str, $matches);
print_r($matches);
//把不是ing结尾的单词找出来
//零宽度 前瞻 断言 负预测
$patt = '/\b\w+(?!ing)\w{3}\b/';//判断不是ing,最起码要有3个以上字符才可以判断
preg_match_all($patt, $str, $matches);
print_r($matches);
//把un开头的单词词根找出来
//零宽度 回顾(返回去看) 正预测  断言
$str = 'luck ,unlucky, state , unhappy';
$patt = '/(?<=\bun)\w+\b/';
preg_match_all($patt, $str, $matches);
print_r($matches);
// 把非un开头的单词找出来
// 零宽度 负预测 回顾 断言
$patt = '/\b\w{2}(?<!un)\w*\b/';
preg_match_all($patt, $str, $matches);
print_r($matches);