Paulo Morgado

.NET Development & Architecture

This Blog

Syndication

Search

Sponsored By

Tags

News

Unit Test Today! Get Typemock Isolator!

Projects

Books

 

Visitors

Visitor Locations

Community

Email Notifications

Archives

Profile

Disclaimer

The opinions and viewpoints expressed in this site are mine and do not necessarily reflect those of Microsoft, my employer or any community that I belong to. Any code or opinions are offered as is. Products or services mentioned are purchased by me, made available to me by my employer or the manufacturer/vendor which doesn't influence my opinion in any way.

Visual Studio Find And Replace Regular Expression Patterns

Visual Studio comes with the a specific set of regular expressions can be used in the Find what field of the its Find and Replace Window.

This page is about a few expressions I've been using over time.

Find And Replace Patterns

Joining Concatenated Strings

I don't like to see constant strings being concatenated in source code. I know the compiler compiles it into a single string, but I think it sends out the wrong message.

However, code generators (like Visual Studio's Unit Test generator) usually generate code with long strings concatenated across several lines of source code.

In order to replace this:

text = "This is a one " + "line concatenation." +
       "This is a multiline concatenation."
       + "This is a multiline concatenation."


       +



       "This is a multiline concatenation with multiple blank lines." +
       "This looks like a string concatenation \" + " + "."

into this:

text = "This is a one line concatenation.This is a multiline concatenation.This is a multiline concatenation.This is a multiline concatenation with multiple blank lines.This looks like a string concatenation \" + ."



The following regular expression can be used:

Find what:
~(\\)\":b*(:b*\n)*\+(:b*\n)*:b*\"
Replace with:
 

There's one caveat, though. This Regular expression only works with regular C# string literals. It doesn't work with Visual Basic or @-quoted C# strings.

Commenting Generated Assert Instructions

Visual Studio's Unit Test generator generates a call to Assert.Inconclusive but, usually generates a call to another method of the Assert class. I find that very annoying because, some times, this makes the test fail instead of being reported as inconclusive.

To comment out these extra calls to Assert methods, the following regular expression can be used:

Find what:
{:b*}{Assert\.~(Inconclusive).*\n:b*Assert\.Inconclusive}
Replace with:
\1//\2

Removing try/finally Tracking Blocks

Sometimes I add some tracking code to help me see what's being executing and when. It's something like this:

public void SomeMethod()
{
  try
  {
    System.Diagnostics.Debug.WriteLine("", ">>> SomeType::SomeMethod()");
    System.Diagnostics.Debug.Indent();

    // Method code.
  }
  finally
  {
    System.Diagnostics.Debug.Unindent();
    System.Diagnostics.Debug.WriteLine("", "<<< SomeType::SomeMethod(Type)");
  }
}

When all is working as expected, this code get's in the way of legibility and maintainability of the code and I just want to get rid of it.

The following pattern searches for code like the above (notice that I'm excluding try/catch/finally blocks because they have a different meaning) to extract the code in the try block without the tracking/debugging code.

Find what:
^:b*try:b*\n:b*\{:b*\n(:b*(System\.Diagnostics\.Debug.*)#\n)*(:b*\n)*{(.*\n)@}:b*\}:b*\n~(:b*catch.*):b*finally:b*\n:b*\{:b*\n(:b*(System\.Diagnostics\.Debug.*)#\n)*:b*\}:b*\n
Replace with:
\1

The resulting code will be something like this:

public void SomeMethod()
{
    // Method code.
}

Visual Studio's Format Document or Format Selection options will then be used to format the code.

Removing #if Tracking Blocks

Other times I'm a bit smarter and I enclose my tracking code inside #if directives, like this:

public void SomeMethod()
{
#if DEBUG
  try
  {
    System.Diagnostics.Debug.WriteLine("", ">>> SomeType::SomeMethod()");
    System.Diagnostics.Debug.Indent();

    // Method code with tracking.
#else 
    // Method code without tracking.
#endif 
    // Method code.
#if DEBUG
  }
  finally
  {
    System.Diagnostics.Debug.Unindent();
    System.Diagnostics.Debug.WriteLine("", "<<< SomeType::SomeMethod(Type)");
  }
#endif 
}

Like before, when all is working as expected, this code get's in the way of legibility and maintainability of the code and I just want to get rid of it.

The following pattern searches for code like the above and extracts only the non-debug code.

Find what:
^:b*\#if:b*(DEBUG|\(DEBUG\)):b*\n(.*\n)@(:b*\#else:b*\n{(.*\n)@})@:b*\#endif:b*\n
Replace with:
\1

The resulting code will be something like this:

public void SomeMethod()
{
    // Method code without tracking.
    // Method code.
}

As usual, Visual Studio's Format Document or Format Selection options will then be used to format the code.

Published Sun, Oct 7 2007 23:30 by Paulo Morgado

Comments

# re: Visual Studio Find And Replace Regular Expression Patterns@ Tuesday, October 02, 2007 8:52 AM

Great Tip for VS.

Ricky.

Ricky

# re: Visual Studio Find And Replace Regular Expression Patterns@ Tuesday, November 18, 2008 11:36 PM

To make static list items Quickly using regular Expression

urenjoy.blogspot.com/.../add-static-itemsmore-than-100-in-list.html

.NET DEv

Leave a Comment

(required) 
(required) 
 
(optional)
(required) 
If you can't read this number refresh your screen
Enter the numbers above: