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

MIX ‘09 With a lot of new stuff

Every big Microsoft event has usually something special for it’s public. And this was the scenario again with Mix 2009. A lot of new CTP releases and other nice bits were introduced.

I’m not going to speak about everything in detail that’s because not everything is that important for me at this point in time. To quickly sum it up what was released, announced:

- Silverlight 3.0 – What’s new in Silverlight 3.0 ? – A guide to Silverlight 3.0 new features
- Expression Blend 3.0 Preview
- ASP.NET MVC RTW 1.0  – Check out Phil Haack’s release post
- Windows Azure March 2009 CTP Tools for Microsoft Visual Studio
- Windows Azure March 2009 CTP Software development kit (SDK)
- Live framework and tools
- Microsoft .NET RIA Services  Preview – What is .NET RIA Services? – Some RIA Services  samples

Currently I’m focused on Windows Azure, ASP.NET MVC.

I’ll start with showing some of the new features in Windows Azure March ‘09 CTP:

What’s new in Windows Azure SDK

  • - Support for developing Managed Full Trust applications. It also provides support for Native Code via PInvokes and spawning native processes.
  • - Support for developing FastCGI applications, including support for rewrite rules via URL Rewrite Module.
  • - Improved support for the integration of development storage with Visual Studio, including enhanced performance and support for SQL Server (only local instance).

What’s new in Windows Azure Tools for Microsoft Visual Studio

  • - Combined installer includes the Windows Azure SDK
  • - Addressed top customer issues
  • - Native Code Debugging
  • - Update Notification of future releases
  • - FastCGI template

From the above list, the really important new features are full trust enabling and next to that FastCGI.

With FastCGI now not only ASP.NET developers but also other developers can place there applications written in PHP for example in Microsoft’s cloud.

I think it’s good that they have provided this possibility with FastCGI. This doesn’t make Microsoft’s cloud only available for Microsoft developers but also for other kinds. This makes Azure more of an open platform.

I will do some more small post on some specific things like ASP.NET MVC

ASP.NET MVC RC2 released

I just noticed a new version of ASP.NET MVC has been released to the public. Don’t think there are many ground breaking changes, except for the deployment process. Next to that several other bugfixes were implemented in this release

Small summary:

- Deployment related updates (Bin deployment and Setup do not require .NET 3.5 SP1) and server-only (no visual studio 2008) install possible.

- JQuery support for the latest version, 1.3.1

- Bug fixing

More information:

Detailed release notes of ASP.NET MVC RC2
Official post from Phil Haack

Download:

ASP.NET MVC RC2 download page
Source code  and MVC Futures assembly