28

I've become rather fond of the "Find-And-Replace" function in Visual Studio. The function lets me replace phrases, words, symbols, or whatever you can type into the textbox on the screen.

How can I make it replace a character with a newline?

3 Answers3

22

According to Helixoft: Multiline Search and Replace in Visual Studio
you can enable Regular Expressions and then use \n to indicate a new line.

2

Select text. Select the little asterisk control immediately to the right of the whole word control, to enable regular expressions. Enter \r\n into find box Enter replacement into "Replace..." Replace all (or replace next)

Doug Null
  • 908
0

If you want to replace a character with a newline, chances are you might also want to replace a newline with a character, or a whole string containing a newline with another string.

I just tried doing this with Visual Studio 2022, and \n in the search pattern did not work. Visual studio kept saying that the regular expression was malformed, but it would not say exactly what the problem was. Extremely frustrating.

So, here is what worked for me:

  1. Uncheck "Match whole word"
  2. Check "Use regular expressions"
  3. Spaces are okay in the search pattern, but if you want to match either spaces or tabs, use \s.
  4. Escape (prepend a backslash to) each of your opening parentheses (().
  5. Escape each of your closing parentheses ()).
  6. Escape any exclamation marks (!) or any other special symbols.
  7. Use .$ to mark the end of the line in the search pattern, not \n.