Find/Replace in Visual Studio with Regular Expressions and Variables

This can be a major time saver when you want to find some text and perhaps duplicate it or manipulate it throughout a bunch of files in your project.

In my case, I was asked to take all the AlternateText properties on Image controls, and duplicate them to ToolTip attributes.  There are ways to accomplish this without actually changing the markup at all, but I wanted to see if Visual Studio could do it for me, and my good friend and colleague, Alex Cibotari, cooked up a solution.


Find: AlternateText='{[^']+}'

Replace With: AlternateText='\1' ToolTip='\1'

You can see that the \1 serves as a variable placeholder for the contents of the first regular expression block in the Find field.

I haven't had the chance to try this out with multiple variables yet, but it makes sense that it would work.




How awesome is that?  Thanks, Alex! 

There are lots of other ways this can be used - let me know if you come up with any cool ones.


  • I recently had to switch the values of two parameters for a few hundred lines of code. I used the following expression:

    Find: {[0-9]+}, {[0-9]+}
    Replace: \2, \1

    So that all the lines of code that looked like this:
    this.Add("EMAIL", 218, 40);

    Became something like this:
    this.Add("EMAIL", 40, 218);

  • some website use this character "|" as a border on menus or phone number, what is the command to specify this character? enclosure for this is possible right? but what is that?

  • Tried this, it did not work, really wish it did.

  • Does anyone know how to use a regex in the "Replace with" without using a literal? I'm trying to replace all instances of variables declared as all uppercase to all lowercase beginning with an underscore.


Comments have been disabled for this content.