199

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?

Alexander Abakumov
  • 10,817
  • 10
  • 71
  • 111
Mantisimo
  • 3,833
  • 5
  • 30
  • 50

6 Answers6

288

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".

Martin
  • 36,545
  • 20
  • 97
  • 129
  • 5
    using the "Play" button to the right of the Find text is handy too – Cel Jan 05 '12 at 13:43
  • 3
    and if you're trying to match special characters such as parentheses dont forget to use e.g. `\)` when regular expressions are on... – Cel Jan 05 '12 at 13:44
  • 2
    The `?` character needs to be escaped in Regex because it signifies that the previous element is optional. Thus, the correct "Find what" text is `~~\?`, with the question mark escaped by a backslash. The answer you gave works on the sample text because it will greedily find the second tilde and consume it, however if a single tilde also exists in the text anywhere, it will improperly be replaced with a newline as well. – ErikE Jul 17 '15 at 18:51
  • If Productivity Power Tools is installed, you can still do ctrl+shit+h and then ctrl+h to open the native Visual Studio Find / Replace window. – Neb Dec 21 '15 at 20:43
  • It should be noted that if "Use Regular Expressions" is selected, one can use "\n" to *find* (and replace) newlines as well. – Jacob Lockard Jun 05 '20 at 12:53
27

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

hkutluay
  • 6,454
  • 2
  • 29
  • 48
Dean
  • 295
  • 3
  • 3
14

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).

detunized
  • 14,409
  • 3
  • 43
  • 62
  • 4
    '~' doesn't need to be escaped in Regex. It's a literal character. This is incorrect. – ErikE Jul 17 '15 at 18:48
11

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).
Suzanne Soy
  • 2,522
  • 5
  • 32
  • 48
  • @Alex If you have a practical solution to this problem which avoids having to manually escape your search & replace text when transforming it to regular expressions, and spend the next 10 minutes fixing the monster created that way, I'd be very glad to hear it, since my answer is just an ugly hack. But as you can see, all other answers that work on a vanilla Visual Studio without extra plug-ins require the use of regular expressions, and thus escaping simple text. – Suzanne Soy Nov 29 '13 at 09:40
  • 2
    I didn't mean that in a bad way. My apologies i forgot to put a smiley :D As for a solution, no i dont have one that involves VS. But I found that any decent external editor that support regex (notepad++ etc) work quite well. – Alex Nov 29 '13 at 09:56
  • 2
    UltraEdit is the king of search replace - here you search for newlines, linebreaks or whatever simply by marking those lines and starting the search. - As for replace you simpy enter ^n for \n, ^r for \r and ^t for \t - if only VS2010 would do the same :-) – Miros Jan 23 '14 at 20:58
  • You can always visit http://regex101.com to see if your search string is going to match literal characters or be interpreted as Regex control characters. – ErikE Jul 17 '15 at 18:53
8

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

enter image description here

Matthew Lock
  • 11,495
  • 11
  • 84
  • 122
Peter Macej
  • 3,781
  • 18
  • 33
  • Does not seem to work with Visual Studio 2017 without some extra steps, i.e. have to click the "find in files" then modify that dialogs scope, the shortcut default is f# interactive now. – Mark Schultheiss Apr 28 '17 at 17:36
2

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

Cryptc
  • 702
  • 1
  • 7
  • 9