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:
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:
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:
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.