multiple lines find replace visual studio Archives - Anuj Varma, Hands-On Technology Architect, Clean Air Activist https://www.anujvarma.com/tag/multiple-lines-find-replace-visual-studio/ Production Grade Technical Solutions | Data Encryption and Public Cloud Expert Sun, 15 Dec 2013 14:26:51 +0000 en-US hourly 1 https://wordpress.org/?v=7.0.2 https://www.anujvarma.com/wp-content/uploads/anujtech.png multiple lines find replace visual studio Archives - Anuj Varma, Hands-On Technology Architect, Clean Air Activist https://www.anujvarma.com/tag/multiple-lines-find-replace-visual-studio/ 32 32 Visual Studio Replace multiple lines of code https://www.anujvarma.com/visual-studio-replace-multiple-lines-of-code/ https://www.anujvarma.com/visual-studio-replace-multiple-lines-of-code/#respond Fri, 29 Nov 2013 14:26:00 +0000 http://www.anujvarma.com/?p=2193 I had to insert a few lines of code in about 700 different files. However, the insertion had to be in a specific place (just before the start of a […]

The post Visual Studio Replace multiple lines of code appeared first on Anuj Varma, Hands-On Technology Architect, Clean Air Activist.

]]>
I had to insert a few lines of code in about 700 different files. However, the insertion had to be in a specific place (just before the start of a transaction – so just before using (var transaction = session.BeginTransaction).
The regular Find and Replace would not work, since I needed to insert a linefeed to separate the lines of code. The linefeed mandated using regular expressions. The replace term would need to contain \n wherever a line feed was needed.

The Search Part

However, even though I had no NEED for regex in my search term, I still HAD to make the search term RegEx friendly. For example, the open parenthesis in using (var , had to be made RegEx friendly.  Fortunately, all this meant was escaping (the open parenthesis ‘(‘) with a backslash – as in \( .
The snapshot shows what my my enusing \(var transaction – replaced with NewLineOfCode1 and NewLineOfCode2 would look like. The \t is used to align the new lines of inserted code with the existing line (using \(var.  The \n is the newline insert between two lines of code.

  1. Notice that ‘Use Regular Expressions’ is checked.
  2. Notice that I still retain the using (var…part – I am doing an insert of additional lines of code – NOT  replacing existing lines of code. The same pattern could be applied for a pure REPLACE as well, of course.
  3. You can optionally apply a filter for choosing the files you apply this to – for e.g. *.cs files etc.

regexfindandreplace

Summary

Using the Regular Expressions Search and Replace allows for replacing / inserting multiple lines of code within multiple source code files in your solution. All you have to do is make your search term RegEx friendly.

The post Visual Studio Replace multiple lines of code appeared first on Anuj Varma, Hands-On Technology Architect, Clean Air Activist.

]]>
https://www.anujvarma.com/visual-studio-replace-multiple-lines-of-code/feed/ 0