Disclaimer

Any opinions expressed here are my own and not necessarily those of my employer (I'm self-employed).

Feb 20, 2012

Multiline search and replace in Visual Studio

Today I had to add a new HttpModule to A LOT of web.configs. Adding it manually would be too tedious, so I had to figure out how to search for a single line in Visual Studio 2010 and replace it with two lines of text. If I could only find a way to search for some text, and replace it with several lines of text!

Google turned up some hints about the Regex search, but no apparent solutions. After playing around a bit with the regex search in Visual studio, I found that it supports multiline text in both the search and replace strings.

When you're using "Find and Replace" in Visual Studio you can enable Regular Expressions, under "Find options", as shown in the screenshot. The regular expressions let you represent newlines in the search or replace fields, by using a \n. Cool! I've included an example below the image.

But beware, when you enable regular expressions you need to escape special characters, in my case < and >,  with a backslash. Your "Find" statement will be interpreted as a regular expression. Check out the MSDN page on regular expressions in Visual Studio to learn more about which characters are special and which are not.

Update Sep. 2012: This post deals with Visual Studio 2010. Regular expressions have changed in VS 2012, as you'll see from the link above. See the comments on this post for a new example of multiline search and replace for both VS 2010 and VS 2012.


In my case, I wanted to add a new module directly after the WIF SessionAuthenticationModule. I've shortened the config lines, for space and readability. To match the config line I had to escape < and > which have special meaning in a regex.

\<add name="SessionAuthenticationModule" ... PublicKeyToken=31bf3856ad364e35" /\>

And you want to replace it with itself, followed by a new line of config (the \N represents the newline):

<add name="SessionAuthenticationModule"... />\n <add name="BrandNewModule" ... />

This neatly replaced the line of config with the two lines of config I was hoping for:

<add name="SessionAuthenticationModule"... PublicKeyToken=31bf3856ad364e35" />
<add name="BrandNewModule" ... />

Easy huh?

***Update***
Mr. F pointed out a better solution in his comment on this post — tagging expressions. Just now, I had to add a new appsetting to a lot of web.configs so I had to give it a try! In the "Find what:"

{\</appSettings\>}

and in the "Replace with:"

<add key="newsetting" value="somevalue"/>\n  \1

Even cooler! Thanks F! (We both know who you are..)

32 comments:

  1. Why not use tagged expressions by adding some curly braces to your search string:
    {\<add name="SessionAuthenticationModule" ... PublicKeyToken=31bf3856ad364e35" /\>}

    and in replace, use this:
    \1\n <add name="BrandNewModule" ... />


    -F

    ReplyDelete
    Replies
    1. Excellent point. If I had read my own reference thoroughly I would've learned this :) The post has been duly updated.

      Delete
  2. This isn't multi-line search though is it? You're replacing with a newline character yes but you're not finding text which spans more than one line. For example, if I want to find:

    one
    two
    three

    and replace it with:

    four
    five
    six

    This can't be done, because VS won't be able to match the first three lines together. Even searching for "one.*two" yields no results which means VS is incapable of seeing beyond one line at a time.

    I appreciate the information provided in this post but its title is misleading, and as a result it comes up when searching for a way to perform multi-line find and replace in VS.

    ReplyDelete
    Replies
    1. Hi,

      thank you for leaving a comment. You're right, I did not include an example for matching text that spans more than one line. It can still be done! The easiest way to match your example in VS 2010 would be this:

      Find: one\ntwo\nthree
      Replace: four\nfive\nsix

      For a more advanced example, where there's whitespace characters before and after each word on the line (think indentation) this would preserve whitespace characters in VS 2010:

      Find: one{[:b\n]*}two{[:b\n]*}three
      Replace: four\1five\2six

      Turns out they changed the regular expressions in VS 2012, so I'll update my post.

      To do the same whitespace preserving multiline search with a multiline replace in VS2012:

      Find: one([\s\n]*)two([\s\n]*)three
      Replace: four$1five$2six

      So, it's possible. Hope this helps.

      Delete
  3. It's also very possible by just opening an editor like dreamweaver and using their multi-line find and replace. Kind of a real pain to go from VS to another application just to get functionality that should be built into VS. The fact that people still have to use hacks to do something that has been included in most other editors for years is amazing.

    ReplyDelete
  4. Thank you very much! :)

    Daniel

    ReplyDelete
  5. Here is VS extension tool for Multiline Search and Replace: https://visualstudiogallery.msdn.microsoft.com/699ce302-b0d4-4083-be0e-1682e873cebf

    ReplyDelete
  6. Wow what a Great Information about World Day its very nice informative post. thanks for the post. Search Bar Firefox 57 Quantum addon

    ReplyDelete
  7. Thank you for the presentation of what is interesting as a variety of comments that are very interesting.


    svetus

    ReplyDelete
  8. Thank you! I hope to hear more updates from you, this is very helpful.
    name generator

    ReplyDelete
  9. Happen to put in your mouth whatever thing this unique chef's positions glued to everybody. These (Michael Kors Outlet) great deviled offspring ended (New Yeezys 2020) quite banging (Jordan Shoes For Sale Online) really perfect, I coulda peed upon sofa (Michael Kors Outlet Online) immediately in view of merrymaking, With dawning really enjoyed hers. For a start, They are surely far originally caused by that mayonaissy subject that have a dust (Ray Ban Outlet Store) of (Cheap Yeezys For Sale) paprika aware that many of us all have a look at (Coach Outlet Online) one's imagination (Coach Outlet Store Online) when we take into consideration deviled ova.

    When the rope over an audio (Cheap Yeezy Shoes Sale) lesson acquiring chant ture over"Bonk police officers, After which detailed herself. "Fully grasp a lot of you around the globe generally go along that preceding track. You (New Jordan Releases 2020) are usually like, 'My mother is truly a police officer.

    Nashua court arrest announced representatives taken care of immediately 18A forest e. To suit guides involving thievery. Their person informed police force he is owned by a facebook or myspace collective described Lawrence shoe goes, Coupled with set to

    ReplyDelete
  10. This is very nice service but many readers had not enough knowledge about it. Now readers have got complete knowledge and they are satisfied. Dissertation writing service.

    ReplyDelete
  11. It’s actually a nice and helpful piece of info. I am glad that you shared this helpful information with us. Please keep us informed like this. Thanks for sharing.

    ReplyDelete
  12. Thanks for some other informative web site. Where else may just I get that type of info written in such a perfect manner? I’ve a project that I am just now working on, and I have been on the glance out for such info.

    ReplyDelete
  13. If you are going for most excellent contents like me, simply go to see
    this site every day for the reason that it presents quality contents, thanks

    Here's my website ::: 안마
    (jk)

    ReplyDelete
  14. Thank you so much for sharing this, I was looking for a tutorial regarding this topic, thanks again! Cheers!

    ReplyDelete
  15. It is one of the temporary activities through which all the people and ideas out here.
    Massage Service in Mumbai

    ReplyDelete
  16. Multiline search and replace option in Visual Studio not only saves time but also significantly reduces the errors that may occur while creating the application. Thank you so much for writing another brilliant post about Gadgets & Tech.

    ReplyDelete
  17. I appreciate the information provided in this post. it's helpful. you share the new HTTP module setting with us. I appreciate the information provided in this post b

    ReplyDelete
  18. Hi all! Looking for expert advice on implementing Dynamics 365 Business Central? Our dedicated outsourcing company offers a step-by-step approach to implementing this powerful ERP solution, helping your organization streamline processes and gain a competitive edge. >> click this link now

    ReplyDelete

Copyright notice

© André N. Klingsheim and www.dotnetnoob.com, 2009-2018. Unauthorized use and/or duplication of this material without express and written permission from this blog’s author and/or owner is strictly prohibited. Excerpts and links may be used, provided that full and clear credit is given to André N. Klingsheim and www.dotnetnoob.com with appropriate and specific direction to the original content.

Read other popular posts