Pattern Search in VS.Net 2010

Recently I came across a fresher recently recruited in my company doing a boring and repetitive work of Find and Replace, doing it manually. He was searching for a particular keyword in the code and replacing a couple of line with the new piece of code. He was not using the VS.Net’s Find and Replace box because he needed to search for a Pattern and replace it with another pattern. He was not able to find a better and more accurate way of doing the same. So I thought of helping him and others who find Pattern search complicated by writing this blog and showing the power of Regex Patterns.

So here we go with this example:

Let’s say we want to replace the ‘Sample Code’ Pattern with the ‘Replace with’ Pattern

Sample Code:

Select Case etime

Case EnumECEnt.EnmDisplayMessageType.Alert

Replace with:

Select Case enmtime

Case EnumECEnt.EnmDisplayMessageType.Warning

We have this common piece of code in lots of classes. I now need to change this. I will open the Find and Replace window and use its Regular Expression option. Below is the screenshot:

Enter the relevant text in Find what and Replace with Text boxes as shown above.

We can then search and replace as usually.

How does this work?

If you notice the text in Find what: box, the lines that we are searching for starts with a ‘\’

\ (backslash) in Regex means match all characters after this. It even suppresses special Regex special characters like – [\^$.|?*+(){}

Since we are looking for multi line texts we need to look for Line breaks. This is done by:

\n” – Match a new line character

So wasn’t this really simple? By using just a couple of Regex keywords we were able to do a complicated Find and Replace which could have required tedious manual effort.

I can be reached at