2017-02-21 32 views
1

如何找到<標籤後面沒有?正則表達式,負向超前(?!?)找到<標籤沒有跟隨?

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

努力時,你應該得到一個'沒什麼repeat'錯誤運行這個... –

+0

是的,錯誤是如你所說。同樣''#<[^?]#''不起作用 – olga

回答

1

?是一個特殊的字符在你的正則表達式模式,應該被轉義:

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

謝謝,作品 – olga