Visual Studio 2010: Find and Replace using Regular Expressions

This is not something introduced by Visual Studio 2010, but I'm a late bloomer. I'm quite sure that at least 2008 and 2005 also have it.

Let's say we have a test class which opens a bunch of files, and that it has a lot of hardcoded relative paths like this:

class FactoryImage{

   public static IEnumerable ImageTestCases{

      get{
         //9 bits, Signed, Monochrome1
         yield return new TestCaseData(new ImageParameters {
             FileName = ".\\ImagesTest\\DavidClunie\\MONOCHROME1\\Signed\\9SignedMonochrome1_512x512_LE.pix", //Rawdata, example image
             FileNameWhite = ".\\ImagesTest\\DavidClunie\\MONOCHROME1\\Signed\\9SignedMonochrome1_512x512_LEWhiteImage.pix", //Rawdata, white example image
             //More fields...
         });

         //9 bits, Unsigned, Monochrome1
         yield return new TestCaseData(new ImageParameters{
            FileName = ".\\ImagesTest\\DavidClunie\\MONOCHROME1\\Unsigned\\9UnsignedMonochrome1_512x512_LE.pix", //Rawdata, example image
            FileNameWhite = ".\\ImagesTest\\DavidClunie\\MONOCHROME1\\Unsigned\\9UnsignedMonochrome1_512x512_LEWhiteImage.pix", //Rawdata, white example image
            //More fields...
         });

         //Lost of yield statements...
      }
   }
}

Now let's suppose that to tidy things up, or because ReSharper told us, or we're just obssesive about source code size, we wish to express all those hardcoded strings as verbatim strings, that is, without escape sequences. What we whish to do is to replace like this:

//This is what we have:
FileName = ".\\ImagesTest\\DavidClunie\\MONOCHROME1\\Signed\\9SignedMonochrome1_512x512_LE.pix",
//And this is what we want:
FileName = @".\ImagesTest\DavidClunie\MONOCHROME1\Signed\9SignedMonochrome1_512x512_LE.pix",

But we have too many lines like these to change them manually. We could replace ". with @" and \\ with \, but that could do more replacements than necessary, and we might need an insane amount of clicks. It's for cases like this that Regex comes to the rescue. There's a reference for the Visual Studio regex syntax here. After checking it out, the replacement for the example can be done using these patterns (click the image to enlarge it):

Needless to say, I didn't write the whole patterns and then started replacing; it's more practical to test the pattern as we write it. That is, write a little part, and click the Find button to see if it matches right. If it does, write a little more and keep going. Once the whole search pattern matches well, start writing the replacement pattern, also progressively. When you test it, hit Replace, and if it doesn't replace correctly, undo using Ctrl+z, fix and try again. The first time might be rough, but once you get the hang of it you'll be far more efficient.

Comments

Popular posts from this blog

VB.NET: Raise base class events from a derived class

Apache Kafka - I - High level architecture and concepts

Upgrading Lodash from 3.x to 4.x