I will give a working example for PHP showing my comments to the question.
I don't know if there are recursive expressions in C #, but they work in PHP.
In this example, in the string, the characters a
inside brackets are replaced with the character c
(on real lines this may be a non-printable character like BELL, etc.).
Then all the remaining a
are replaced by X
, and c
back to a
.
That is, in accordance with the question, the analogy is:
\n = a ; CHR(8) = c ; <BR/> = X
<? header("Content-Type: text/html; charset=utf8;"); $re='/\{(?>[^}{]+|(?R))+\}/'; $s= <<< HEREDOC bab{ab{bab}}bab{{{{{ab}}}}}{aba HEREDOC; function clb($match){ return str_replace("a", "c", $match[0]); }; $result=preg_replace_callback($re, clb, $s); $result=str_replace("a", "X", $result); $result=str_replace("c", "a", $result); echo '<pre>'.$result."</pre>"; ?>
Result. As you can see, all a
outside the brackets are replaced by X
bXb{ab{bab}}bXb{{{{{ab}}}}}{XbX
Sorry for nekropravku, something this question was remembered.
You can do this without any extra replacements:
$re='/\\n|(?P<rec>\{(?>[^}{]+|(?P>rec))+\})/'; function clb($match){ if ($match["rec"]) return $match["rec"]; return "<br/>"; }; $result=preg_replace_callback($re, "clb", $text);
In general, it looks like this:
$re="/expression1|(?P<name>expression2)/"; function callback($arr) { if ($arr["name"]) return $arr["name"]; return $replace; // здесь то, чем нужно заменить первое выражение };
Now I myself am surprised at how simple it turns out, but at the time of writing the answer, everything seemed much more difficult to replace outside of a regular expression.