Geeks With Blogs
A Technical Debtor Toward continuous improvement

I had a case today where I wanted to find all the spots within our application that a developer had explicitly set the BackColor property of a control at run-time. As you can imagine, a simple search ended up with my finding a ton of designer code where the generated code was setting the property. (It sure would be nice if Visual Studio would let you exclude comments and designer files from the search – but that’s not in VS2008 or VS2010 Beta 1, unless it’s well hidden in both.)

Wildcards seems like a fairly good starting point, but I couldn’t think of a way to exclude particular strings using wildcards… so it was time to delve into the other options – “Use Regular Expressions.” (This option is exposed through the combo box at the bottom of the Find/Replace dialog, as shown below.)


To be perfectly frank about it, I approach regular expressions with quite a bit of trepidation. It’s not so much that I don’t…. well, okay, I’ll admit it. I just plain don’t understand them. It’s not that the concept it hard – it’s that every implementation of RegEx I’ve seen has a different syntax. And I’ve never found a particularly definitive syntactical lexicon for .NET regular expressions with useful expressions.

So, it was off to Google and MSDN help. Neither of these was as helpful as I would have liked. The piece I didn’t see with a quick scan? Enclosing a string literal in parentheses makes the RegEx engine treat that as a phrase.

After poking around a little bit, I was able to cobble together something that was really helpful:

~((Appearance|Shape):a#)(.BackColor = )~(System|True|False)

Now, that may look rather impenetrable, so I’ll walk through it. We use Infragistics controls, so there’s dozens, if not hundreds, of Appearance objects in the designer-generated code. The ~ is a negation character. The pipe (|) is a logical OR. The phrase “:a#” means “match any number of alphanumeric characters in this position.” (In retrospect “:d#” might have been better, since that would match on numbers only.) So, “~((Appearance|Shape):a#)” translates as “don’t show me any matches that include Appearance## or Shape##”.

The next piece of the RegEx specifies that I’m looking for instances of “.BackColor = “. Finally, for some Infragistics-specific reasons, it helped clear out the noise if I excluded (~) any property settings that were Boolean values (True|False). (Remember that True and False are treated as string literals here, not Boolean values).

This really helped me narrow down what I was looking for – not a matter of trivial importance when searching through a few hundred thousand lines of code across 75 or so projects!

Of course, after the fact, I thought about checking to see if Sara Ford has a tip for this on her blog. Of course she did! Where I hand-rolled my RegEx, she shows how to use the expression builder to do the same.

Hopefully, this will help demystify RegEx searches in Visual Studio a little bit, and save you some time!

Posted on Thursday, September 24, 2009 10:35 AM Tips and Tricks , VSTS , DevCenter | Back to top

Comments on this post: Regular Expressions in Visual Studio “Find”

# re: Regular Expressions in Visual Studio “Find”
Requesting Gravatar...
Thanks for a very useful post. I was hoping to searh for particular search terms which had NOT been commented out.

To exclude comments from your rexed search, use


which effectively means at the beginning of the line exclude any amount of white space (to allow for auto-indented comments) followed by the apostrophe comment character.

To find conditional stops that have not been commented out use


Left by Phil Maynard on Jul 01, 2010 1:44 AM

Your comment:
 (will show your gravatar)

Copyright © Jeff Certain | Powered by: