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();
   6:             var localizedString =  user==null? resMan.GetString(key) : 
   7:                 resMan.GetString(key, user.CultureInfo );
   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:             }
  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:


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

Why is this install asking for .NET 3.5 …

I’m not using any code that’s specific to .NET 3.5 framework. I am however developing my application using Visual Studio 2008. Yes I have specified the target framework as .NET2.0. Still my installer that my setup project created is asking for .NET3.5.

Normally this is no big deal, just download the core .NET3.5 framework files, about 70MB and your done. But let’s say that you don’t have the system requirements for .NET3.5, for example Windows2000. You can’t install .NET 3.5 on Windows2000.

So I did some investigation on the setup project. Because I was absolutely sure I did not write any code that required .NET3.5 it had to be there somewhere.

In your Setup Project you need to look for Deteched Dependencies. There is a node called Microsoft .NET Framework, double click this and look at the property window:


As you can see, you can specify the required .NET framework there. Change this to the framework you desire and you’re ready to go!

Talking about Visual Studio 2008 SP1 and .NET 3.5 SP1

image Today 2 new SP1′s arrived on the scene. One for Visual Studio 2008 and one for Microsoft’s development .NET3.5 framework. It’s a good thing too, the framework has some cool enhancements for the traditional languages like C#, VB but also for WPF, ASP.NET 3.5 etc..


.NET Framework version 3.5 Service Pack 1 provides the following new features and improvements:

- ASP.NET Dynamic Data, which provides a rich scaffolding framework that enables rapid data driven development without writing code, and a new addition to ASP.NET AJAX that provides support for managing browser history (back button support). For more information, see What’s New in ASP.NET and Web Development.

- Core improvements to the CLR (common language runtime) that include better layout of .NET Framework native images, opting out of strong-name verification for fully trusted assemblies, improved application startup performance, better generated code that improves end-to-end application execution time, and opting managed code to run in ASLR (Address Space Layout Randomization) mode if supported by the operating system. Additionally, managed applications that are opened from network shares have the same behavior as native applications by running with full trust.

- Performance improvements to WPF (Windows Presentation Foundation), including a faster startup time and improved performance for Bitmap effects. Additional functionality for WPF includes better support for line of business applications, native splash screen support, DirectX pixel shader support, and the new WebBrowser control.

- ClickOnce application publishers can decide to opt out of signing and hashing as appropriate for their scenarios, developers can programmatically install ClickOnce applications that display a customized branding, and ClickOnce error dialog boxes support links to application-specific support sites on the Web.

- The Entity Framework is an evolution of the existing suite of ADO.NET data access technologies. The Entity Framework enables developers to program against relational databases in according to application-specific domain models instead of the underlying database models. For more information, see Getting Started with the Entity Framework. The Entity Framework introduces some additional features, including support for new SQL Server 2008 types, default graph serialization of Entities, and the Entity Data Source. This release of the Entity Framework supports the new date and file stream capabilities in SQL Server 2008. The graph serialization work helps developers who want to build Windows Communication Foundation (WCF) services that model full graphs as data contracts. The Entity Data Source provides a traditional data source experience for ASP.NET Web application builders who want to work with the Entity Framework.

- LINQ to SQL includes new support for the new date and file stream capabilities in SQL Server 2008.

- The ADO.NET Data Services Framework consists of a combination of patterns and libraries, which enable data to be exposed as a flexible REST (Representational State Transfer)-based data service that can be consumed by Web clients in a corporate network or across the Internet. The ADO.NET Data Services Framework makes data service creation over any data source. A conceptual view model of the underlying storage schema can easily be exposed through rich integration with the ADO.NET Entity Framework. Services created by using the ADO.NET Data Services Framework, and also compatible Windows Live ( services, can be easily accessed from any platform. For client applications that are running on Microsoft platforms, a set of client libraries are provided to make interaction with data services simple. For example, .NET Framework-based clients can use LINQ to query data services and a simple .NET Framework object layer to update data in the service.

- Windows Communication Foundation now makes the DataContract Serializer easier to use by providing improved interoperability support, enhancing the debugging experience in partial trust scenarios, and extending syndication protocol support for wider usage in Web 2.0 applications.

- The .NET Framework Data Provider for SQL Server (SqlClient) adds new support for file stream and sparse column capabilities in SQL Server 2008.

If you want more information on which exact bugs were fixed, check out KB951847: List of changes and fixed issues for Visual Studio 2008 Service Pack 1 Beta for the .NET Framework 3.5


ASP.NET in the .NET Framework 3.5 Service Pack 1 release includes numerous bug fixes. In addition, it includes features for the following:

- Enabling high-productivity data scenarios by using ASP.NET Dynamic Data.

- Supporting the browser navigation in ASP.NET AJAX applications by using ASP.NET AJAX browser history.

- Increasing the download speed for ASP.NET applications by using ASP.NET AJAX script combining.

Check out the following link if you want to look at screencasts about the new ASP.NET 3.5 enhancements (Dynamic data, ASP.NET AJAX browser history, ASP.NET AJAX script combining, ADO.NET Data Services, ADO.NET Entity Framework)

I’m also very happy with the SP1 for Visual Studio 2008. I wanted to try out SQL Server 2008, which came out a couple days ago. But there seems to be a problem when you try to install  SQL Server 2008 with Visual Studio 2008 (without SP1). They have conflicting components. If you check out the release notes of SQL Server 2008 you can see that Visual Studio 2008 SP1 is a requirement for SQL Server 2008.

One other tip I have to give you is that if you want to install Visual Studio 2008 SP1 and you have beta version of the service pack, please download an use the Visual Studio 2008 Service Pack Preparation Tool. This tool deletes all those previous versions.

Ok so now you have all the necessary information so go ahead and:

Download Visual Studio 2008 SP1 (which also installs .NET 3.5 SP1)

Download .NET 3.5 SP1

Other informative links:

Official Visual Studio 2008 SP1 homepage