olga
olga

Reputation: 969

regex, negative lookahead (?!?) find < tags not followed by?

How to find < tags not followed by ?

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

Upvotes: 1

Views: 102

Answers (1)

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

? is a special character in your regexp pattern and should be escaped:

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

Upvotes: 1

Related Questions