Pet*_*tai 2 php regex regex-negation
将not ^运算符与back引用结合使用时,为什么需要使用惰性匹配?似乎not应该打破比赛.
例如:
<?php
preg_match('/(t)[^\1]*\1/', 'is this test ok', $matches);
echo $matches[0];
?>
Run Code Online (Sandbox Code Playgroud)
this testthis t尽管中间t不匹配,否则将输出而不是输出[^\1].我需要/(t)[^\1]*?\1/用来匹配this t.
此外
preg_match('/t[^t]*t/', 'is this test ok', $matches);
Run Code Online (Sandbox Code Playgroud)
发生了什么事,我误解了什么?
它不起作用,因为\1这里不是字符类中的反向引用.它\1被解释为ASCII值为1的字符.
您可以使用负面外观来获得所需的效果:
'/(t)(?:(?!\1).)*\1/'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
97 次 |
| 最近记录: |