正则表达式,负向前瞻(?!?)找到<标签没有跟着?

时间:2017-02-21 12:16:36

标签: php regex lookahead

如何查找未跟<

?代码
$htmlStr = " ba <div>b <? </div>n";
$regex1 = '#<#'; // finds 3 '<' 
$regex2 = '#<(?!?)#'; // does not find anyhting, although should find two '<' not followed by '?' 

1 个答案:

答案 0 :(得分:1)

?是你的正则表达式模式中的一个特殊字符,应该被转义:

$htmlStr = " ba <div>b <? </div>n";
$regex2 = '#<(?!\?)#';   // <-- will find 2 matches