Unfortunately, not strong in regular expressions. Therefore, I ask for help in an expression that will search for characters \n only outside, say {...} , that is, if there is a block of text within curly brackets, they do not search for a newline character, ONLY outside:

 ...ищем...{...нет...}...снова ищем..{...ишем?...{...нет...}...ищем... 

Thank you in advance.

  • I would suggest to solve this way: first, find all curly brackets, including nested ones, well, everything that does not fall under this regular expression will find the necessary \ n. A similar regular expression is described in [Khabrapost] [1], only instead of {} they are searched for (). [1]: habrahabr.ru/post/56765 - ReinRaus
  • for some reason did not think about it, I will take note, thank you, but I still hope for the finished regular season =) - Specter

1 answer 1

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.

  • for some reason I did not think to go this way with the repeated replacement of characters back, thanks for the idea - Specter
  • no matter how hard he tried to study regulars, they still look like something unintelligible to me =) thanks again! - Specter