Suppose there is a text text1(text2)text2 you need to get text1 (text2) text2

Tried to do through the replacement on the regular basis: ([^\s])([\(])|([\)])([^\s])

 $1 $2$3 $4 

but I get text1 (text2) text3

  • Where did the minusator come from? ((( - Qwertiy
  • one
    Is a regular expression needed here? You can just use a replacement. - kmv
  • @kmv, you can, but I need it for an example. Since | I did not get the result when working with groups, which I expected. If | split into 2 regulars, then everything works as expected. - iluxa1810
  • I deleted the answer, since the question is not clear, please clarify all contexts in which you need to add spaces. - Wiktor Stribiżew

4 answers 4

Let me explain where the extra spaces come from as a result.

Regularity works twice, as there | .

For the first time, the text before the opening bracket and the bracket itself, respectively, will fall into parameters $ 1 and $ 2. And the parameters $ 3 and $ 4 will remain empty. Therefore, it will output:

text to the bracket, space, opening bracket, empty, space (extra!), empty.

The second time, on the contrary, the first two parameters will remain empty, and the last two will capture the closing bracket and the text after it. Similarly, it will display:

empty, space (extra!), empty, closing bracket, text after the bracket.


I suggest using the Replace method overload using MatchEvaluator . In this case, the regular order is the simplest. But I had to use a dictionary with pairs of substitutions.

 var dict = new Dictionary<string, string> { ["("] = " (", [")"] = ") " }; string input = "text1(text2)text3"; string pattern = @"\( | \)"; var options = RegexOptions.IgnorePatternWhitespace; string output = Regex.Replace(input, pattern, m => dict[m.Value], options); Console.WriteLine(output); 

Comments to other answers suggested that spaces should be added only if they are not present. Probably for this the author used ([^\s]) . In this case, for my version, the regular schedule will be as follows:

 @"(?<!\s) \( | \) (?!\s)" 

    Try not to catch the text to the brackets | after, and the very central part:

    Pattern: \s?\((.*)\)\s?

    Replacing: " ($1) " // notice the spaces

    Example

    • The first adequate answer ... - Pavel Mayorov
    • @PavelMayorov, how is it adequate if it doesn’t do that with regex101.com/r/vB8zH7/1 ? - Visman
    • one
      @Visman was persuaded, corrected it again :) And it is adequate in that it gives at least some general advice instead of dumping the finished expression with a bunch of brackets, some of which are screened ... - Pavel Mayorov
    • @PavelMayorov, you can fix it 10 more times, there’s no sense in regex101.com/r/vB8zH7/2 - Visman
    • Well, you are inside my answer, changing the task on the go. Initially it was easy to separate the text in brackets with spaces. and by changing the text, it is possible to break any regular :) regular expression works where there is at least some format. - GRUNGER

    You can replace (?=\()|(\)) With $1 .

    If you need to make one space, regardless of whether there were spaces, or not, then replace \s*(?:(\()|(\)))\s* with $2 $1 .

    If you do not need to leave spaces between the same brackets, then \s*(?:(\(+)|(\)+))\s* on $2 $1 .

    • With this approach, spaces will be added even if they are available, see here . That is why the TS used in the expression [^\s] . - Wiktor Stribiżew
    • @ WiktorStribiżew, the answer is supplemented. - Qwertiy
    • I do not argue, I also thought about this option, but I’m not sure if you need to remove existing spaces (for example, in text1ПРОБЕЛ_ПРОБЕЛ(text2) text2 ). - Wiktor Stribiżew

    I do not know whether such a replacement will work in C #.

    Regular expression to search

     (?<=[^\(\s])(?=\()|(?<=\))(?=[^\)\s]) 

    Replacement: space character (!!!)

    Test here https://regex101.com/r/cV6oQ7/1

    PS A regular does not select anything on its own, but only finds positions between the opening bracket and the symbol in front of it not equal to the whitespace character or opening bracket OR the position between the closing bracket and the symbol behind it is not equal to the closing bracket or white space character.

    • @ iluxa1810, do you even comment on whether the regular schedule works without capturing characters? - Visman
    • in fact, of course, I do not understand why there is a minus here - GRUNGER