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:
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:
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:
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:
For the replace box we’ll use the following regular expression:
Let’s dissect the first regular expression:
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:
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:
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:
Looking for more detailed information on this, check out: How to: Search with Regular Expressions on msdn