php正则预查

php正则预查

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

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

原文地址:https://www.cnblogs.com/xiong63/p/6102845.html