Using regular expression search/replace in Visual Studio

By | 2009-04-14

Note: I have transferred this blog from my old blog site: http://dcarapic.blogspot.com/ so I am reposting most of the stuff from there

In the previous post I have shown you how to do search/replace using .NET Regex. Visual Studio 'Find and Replace' dialogue (CTRL+H) also support regular expression search and replace and if you learn to use it (or better said, if you get into the habit of using it) you can do some pretty powerful stuff.

Visual Studio 2008 Search replace dialog

Note: Wherever you see me write 'enter "xyz" into some text field', it is assumed that you should enter it without the quotation marks
Note 2: Also all 'questions' here are simply a figment of my imagination, nobody asks me anything 🙁

Q: I have pasted a bunch of code from the web into Visual Studio but I get double newlines, is there a way to strip them?
A: Why of course. Open the Find and replace dialogue. Enter \n+ into "Find what" field and \n into "Replace with" field (do not forget to check the "Use: Regular expressions").
E: We say to VS: find any one or more continuous occurrences of newline character and replace it with just one newline character

The really interesting thing about regular expression search/replace is the usage of the grouping constructs to do a 'capture' and then use that capture in the replacement pattern. Great thing is that Visual Studio supports this! Unfortunately Visual Studio regular expression grouping constructs are not the same as .NET grouping constructs.
They are not difficult to learn but it is annoying that Microsoft did not settle for one standard (at least for them) way to use regular expression groupings. Instead of using parenthesis - (, ) to define capture groups you must use curly braces - {, } Instead of using dollar $ sign for using captures inside the replacement you must use the backslash \ character.

Q: I have a bunch of forms/web pages where I have methods where I first load data from an object to controls and then back from controls into an object. Do I have to type everything?
A: Of course not! We can copy the code we have and then use Find and replace to swap the assignment lines (something = somethingelse; into somethingelse = something;). For example we have the following method:

public static void FillControls(SomeObject o)
{
    txt1.Text = o.Property1;
    txt2.Text = o.Property2;
    txt3.Text = o.Property3;
    txt4.Text = o.Property4;
    txt5.Text = o.Property5;
    txt6.Text = o.Property6;
    txt7.Text = o.Property7;
    txt8.Text = o.Property8;
}

In order to create an 'oposite' method, we first make a copy of the first method and rename it:

public static void FillObject(SomeObject o)
{
    txt1.Text = o.Property1;
    txt2.Text = o.Property2;
    txt3.Text = o.Property3;
    txt4.Text = o.Property4;
    txt5.Text = o.Property5;
    txt6.Text = o.Property6;
    txt7.Text = o.Property7;
    txt8.Text = o.Property8;
}

Then we select the lines where we want to make a swap, hit CTRL+H to popup the Find and replace dialog, enter {.*} = {.*}; into the "Find what:" field, enter \2 = \1; into the "Replace with:" field, hit "Replace All" button and ... :

public static void FillObject(SomeObject o)
        {
o.Property1 =             txt1.Text;
o.Property2 =             txt2.Text;
o.Property3 =             txt3.Text;
o.Property4 =             txt4.Text;
o.Property5 =             txt5.Text;
o.Property6 =             txt6.Text;
o.Property7 =             txt7.Text;
o.Property8 =             txt8.Text;
        }

E: We say to VS: find a group of characters before the = and assign it to a group (1), then find a group of characters after the = and assign it to a group (2); in the replacement first place the contents of the group (2), then the = and then contents of group (1)

We might experiment with the regular expression and get a better formatted version but it is easier to just hit the "Format the whole document" button (CTRL+E, D) which will format our code nicely.

Leave a Reply

Your email address will not be published. Required fields are marked *