In applying the use case structure presented in this question and the response by Boris the Spider, I came up with this solution
$detail = '[code]<!doctype>
<html>
<head></head>
<body><p>My Regex script</p></body>
</html>[/code]';
function regex($detail)
{
if(preg_match('#^\[code](?=.*(<([A-Z][A-Z0-9]*)\b[^>]*>(.*?)</\2>))\[/code]#si', $detail))
{
return true;
}
return false;
}
echo regex($detail);
Taking a look inside the regex engine, this is what is happening. In applying ^\[q](?=.*(<([A-Z][A-Z0-9]*)\b[^>]*>(.*?)</\2>))[/q]*
to $detail above; \[q]
matches [q]
and the html section of the code matches (<([A-Z][A-Z0-9]*)\b[^>]*>(.*?)</\2>))
.
The match from the lookahead is discarded, so the engine steps back from [/q] in the string to the html section of the code. The lookahead was successful, so the engine continues with [/q]
. But [/q]
cannot match (<([A-Z][A-Z0-9]*)\b[^>]*>(.*?)</\2>))
. So this match attempt fails.
However this regex synthax works:
#^\[code](?=.*(<([A-Z][A-Z0-9]*)\b[^>]*>(.*?)</\2>\[/code]))#si
The regex engine simply says: "opening [code]
tag followed by any character, then a pair of html tags with at least a closing [/code]
tag at the end".
I hope this helps explain the use case pattern match (quit) more.