Visual Studio 2008 Find and Replace using Regular Expressions and Tagged Expressions

Did you ever need to replace something on a lot of places in your code and you couldn’t do a simple find and replace. For instance you needed some parts of the original data and replace that with new data containing the parts found in the original data.

For instance: You have a lot of values in a resources project somewhere in your solution. Normally you would call your resource using the following line:

   1: MyNamespace.MyResourceProject.MyResourceFile.MyResourceValue.EncodeResource()

This is what we used on numerous places in our code.

Now we wanted to localize our messages with specific resource files for each culture and we wanted to do some exception handling.
We wanted to do this using a Type safe generic method. This method would grab the culture from the currently logged in user, use the resource manager and grab the correct resource file. The method looks like this:

   1: public static string LocalizeString<T>(string key)
   2:         {
   3:             var resMan = new ResourceManager(typeof(T));
   4:             var user = (MyUserObject) Membership.GetUser();
   5:             
   6:             var localizedString =  user==null? resMan.GetString(key) : 
   7:                 resMan.GetString(key, user.CultureInfo );
   8:  
   9:             if(string.IsNullOrEmpty(localizedString))
  10:             {
  11:                 BusinessLogging.Instance.Error(string.Format("No string found for key '{0}' in {1}.", key, user.CultureInfo.Name ));
  12:             }
  13:  
  14:             return localizedString.EncodeResource();
  15:         }

Ok so we wanted to call this static method with the correct Type ( f.e. MsHelp.Resources.Users) and request the correct value (f.e. InsertUser). The call to the static method would look like this then:

   1: MsHelp.Resources.Users.InsertUser.EncodeResource();
   2: //Should become
   3: ResourceLocalizer.LocalizeString<MsHelp.Resources.Users>("InsertUser");

As you can see a simple find and replace would not be possible. Every single line in the code using a resource needs to be checked and edited manually. I wanted to automate this process and make it as generic as possible.

This can be done using Regular Expressions. In Visual Studio 2008 (maybe 2005 as well) you can make use of regular expressions and use tagged expressions from your find in the replace part. This means that you can look for something, specify one or more tagged expressions and use this in the to replace part.

So I created a regular expression to look for the first usage (MsHelp.Resources.Users.InsertUser.EncodeResource();) and replace this with (ResourceLocalizer.LocalizeString<MsHelp.Resources.Users>(“InsertUser”);)

The regular expression for the find box will be:

   1: {MSHelp.Resources\.[a-zA-Z]+}\.{[a-zA-Z]+}\.EncodeResource\(\)

For the replace box we’ll use the following regular expression:

   1: ResourceLocalizer.LocalizeString<\1>("\2")

 

Let’s dissect the first regular expression:

{MSHelp.Resources\.[a-zA-Z]+}\.{[a-zA-Z]+}\.EncodeResource\(\)

The part in red will be our first tagged expression and the part in green will be our second tagged expression. The first tagged expression can be used in the replace field using \1 and the second \2. This can be seen in the second regular expression (for the replace field).

Now sometimes I don’t put the namespace (MSHelp) in my call to my resources and sometimes I don’t call the EncodeResource() method. Ok so this a final thing that I want to change in my find regular expression. You can solve this using the * behind the parts (placed betwee braces) that you may or may not have placed in your code . This will look for one or more occurrences. The regular expression now looks like this:

   1: (MSHelp\.)*{Resources\.[a-zA-Z]+}\.{[a-zA-Z]+}(\.EncodeResource\(\))*

The last thing that I’m going to do is implement some more consistency. I want to replace my found result to always include the namespace. The replace regular expression will look like this:

   1: ResourceLocalizer.LocalizeString<MSHelp.\1>("\2")

Ok that’s it! This is a very custom regular expression can be used for many more things, just adjust to your own needs. Also Visual studio can be used fairly easy to create a regular expression. In the find and replace window just click on the arrows on the right for all the different expressions that you can use:

 

Regular expression options in Visual Studio 2008

Looking for more detailed information on this, check out: How to: Search with Regular Expressions on msdn


No comments yet. Be the first.

Leave a reply

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word