(Print this page)

Visual Studio and SSMS: using Regular expressions to search for terms
Published date: Monday, October 26, 2020
On: Moer and Éric Moreau's web site

For quite some time now (I should say many years), I had a very simple task on my TODO list (mine is particularly long!).

This little task was simply: In Visual Studio, how to search for a word which is not on a commented line?

That would have been nice if the VS IDE would offer that option built-in, but it is sadly not! Another useful option would be to colorize the search results exactly like the code window does.

This morning, I was working in a new project for a new client and find they are not deleting lines of code very often, they are most of the time commenting out the code (even if the code hosted in TFS). I was searching for a word and the Find in files feature (CTRL-SHIFT-F) returned literally many hundreds of rows, most of them being commented out. It was nearly impossible to find out the line of codes (about 20) in this non-ending results.

I opened a browser and quickly found an old solution on stack overflow that is working great. The trick is to use a regular expression in the “Find what” textbox and to check the “Use Regular Expressions” under “Find options”. The very same trick also works for the regular search feature (CTRL-F).

The other nice thing about this trick is that using the same idea (with slight difference in syntax), it works in VB.Net, C#, and even in SSMS.

The regular expression to use are (depending on language and/or IDE):

VB.Net
^(?![ \t]*').*YourSearchTerm
C#
^(?![ \t]*//).* YourSearchTerm
SQL
^(?![ \t]*--).*YourSearchTerm

I know, there are more ways to comment out code (like /* … */) for which this trick won’t work. But at least for today since I don’t have that kind in the code I am looking at, I will stop there. If you find a better solution, let me know!


(Print this page)