Find and replace - Add carriage return OR Newline

Visual StudioReplaceCarriage Return

Visual Studio Problem Overview


In the case of following string to be parsed.

ford mustang,10,blue~~?bugatti veyron,13,black

I want to replace the ~~? with a carriage return

Replacing with \n just adds the string "\n"

How can this be done?

Visual Studio Solutions


Solution 1 - Visual Studio

Make sure "Use: Regular expressions" is selected in the Find and Replace dialog:

Find/Replace Dialog Use Regular expressions

Note that for Visual Studio 2010, this doesn't work in the Visual Studio Productivity Power Tools' "Quick Find" extension (as of the July 2011 update); instead, you'll need to use the full Find and Replace dialog (use Ctrl+Shift+H, or Edit --> Find and Replace --> Replace in Files), and change the scope to "Current Document".

Solution 2 - Visual Studio

You can also try \x0d\x0a in the "Replace with" box with "Use regular Expression" box checked to get carriage return + line feed using Visual Studio Find/Replace. Using \n (line feed) is the same as \x0a

Solution 3 - Visual Studio

If you set "Use regular expressions" flag then \n would be translated. But keep in mind that you would have to modify you search term to be regexp friendly. In your case it should be escaped like this "~~?" (no quotes).

Solution 4 - Visual Studio

If you want to avoid the hassle of escaping the special characters in your search and replacement string when using regular expressions, do the following steps:

  1. Search for your original string, and replace it with "UniqueString42", with regular expressions off.
  2. Search for "UniqueString42" and replace it with "UniqueString42\nUniqueString1337", with regular expressions on
  3. Search for "UniqueString42" and replace it with the first line of your replacement (often your original string), with regular expressions off.
  4. Search for "UniqueString42" and replace it with the second line of your replacement, with regular expressions off.

Note that even if you want to manually pich matches for the first search and replace, you can safely use "replace all" for the three last steps.

Example

For example, if you want to replace this:

public IFoo SomeField { get { return this.SomeField; } }

with that:

public IFoo Foo { get { return this.MyFoo; } }
public IBar Bar { get { return this.MyBar; } }

You would do the following substitutions:

  1. public IFoo SomeField { get { return this.SomeField; } }XOXOXOXO (regex off).
  2. XOXOXOXOXOXOXOXO\nHUHUHUHU (regex on).
  3. XOXOXOXOpublic IFoo Foo { get { return this.MyFoo; } } (regex off).
  4. HUHUHUHUpublic IFoo Bar { get { return this.MyBar; } } (regex off).

Solution 5 - Visual Studio

You can use Multiline Search and Replace in Visual Studio macro which provides nice GUI for the task.

enter image description here

Solution 6 - Visual Studio

Just a minor word of warning... a lot of environments use, or need, "\r\n" and not just "\n". I ran into an issue with Visual Studio not matching my regex string at the end of the line because I left off the "\r" of "\r\n", so my string couldn't match with a missing invisible character.

So, if you are doing a find, or a replace, consider the "\r".

For a little more detail on "\r" and "\n", see: https://stackoverflow.com/a/3451192/4427457

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMantisimoView Question on Stackoverflow
Solution 1 - Visual StudioMartinView Answer on Stackoverflow
Solution 2 - Visual StudioDeanView Answer on Stackoverflow
Solution 3 - Visual StudiodetunizedView Answer on Stackoverflow
Solution 4 - Visual StudioSuzanne SoyView Answer on Stackoverflow
Solution 5 - Visual StudioPeter MacejView Answer on Stackoverflow
Solution 6 - Visual StudioCryptcView Answer on Stackoverflow