Ce n'est pas vraiment une réponse parce que vous avez demandé une expression régulière, mais je viens d'écrire une fonction vraiment sale de le faire:
<?php
$html = ' <pre>hello > <</pre>
> <
<pre></pre>';
function stringReplaceThing($str) {
$offset = 0;
$num = 0;
$preContents = array();
$length = strlen($str);
//copy string so can maintain offsets/positions in the first string after replacements are made
$str2=$str;
//get next position of <pre> tag
while (false !== ($startPos = stripos($str, '<pre>', $offset))) {
//the end of the opening <pre> tag
$startPos += 5;
//try to get closing tag
$endPos = stripos($str, '</pre>', $startPos);
if ($endPos === false) {
die('unclosed pre tag..');
}
$stringWithinPreTags = substr($str, $startPos, $endPos - $startPos);
//replace string within tags with some sort of token
if (strlen($stringWithinPreTags)) {
$token = "!!T{$num}!!";
$str2 = str_replace($stringWithinPreTags, $token, $str2);
$preContents[$token] = $stringWithinPreTags;
$num++;
}
$offset = $endPos + 5;
}
//do the actual replacement
$str2 = str_replace(array('>', '<'), array('>', '<'), $str2);
//put the contents of <pre></pre> blocks back in
$str2 = str_replace(array_keys($preContents), array_values($preContents), $str2);
return $str2;
}
print stringReplaceThing($html);
Malheureusement php regex ne vous permet pas de mettre des affirmations et préanalyse lookbehind à l'intérieur d'une classe de caractères. Je n'ai pas testé votre code ci-dessus mais cela ressemble à un cauchemar de compilation (au moins en PHP). Aussi php nécessite que vos assertions soient fixées en largeur. – localshred