How can I search in Visual Studio and get it to ignore what is commented out?

10,676

Solution 1

delete the commented out code, it is in source control right? there is no need to keep it in the file as well.

Solution 2

As the other provided solutions didn't work for me, I finally discovered the following solution:

^~(:b*//).*your_search_term

Short explanation:

  • ^ from beginning of line
  • ~( NOT the following
  • :b* any number of white spaces, followed by
  • // the comment start
  • ) end of NOT
  • .* any character may appear before
  • your_search_term your search term :-)

Obviouly this will only work for // and ///-style comments.

You must click "Use Regular Expressions " Button (dot and asterisk) on your find window to apply regex search

Solution 3

My take:

yes you can use regular expressions, those tend to be too slow and thinking about them distracts from focusing on real stuff - your software.

I prefer non-obtrusive semi-inteligent methods:

Poor man's method: Find references if you happen to use intelisense on

Or even better: Visual assist and it's colored "Find all References" and "Go To" mapped to handy shortcuts. This speeds up navigation tremendously.

Solution 4

Previous answer gave a false-positive on cases where otherwise matching lines were placed on lines containing other source:

++i; // your_search_term gets found, don't want it found

So replaced the :b* with .* and added the <> so only entire words are found, and then went after some of the older C-style comments where there's a /* on the line:

^~(.*//)~(.*/\*).*<your_search_term>

In my case I was hunting for all instances of new, not amenable to refactor assistance, and boatloads of false-positives. I also haven't figured out how to avoid matches in quoted strings.

Solution 5

Just to add on, as I was doing a "find all" for division operator used in the code, used the below to exclude comments as well as </ and /> from aspx files:

^~(.*//)~(.*/\*)~(.*\<\/)~(.*/\>).*/
Share:
10,676
RD.
Author by

RD.

Updated on June 23, 2022

Comments

  • RD.
    RD. almost 2 years

    I am refactoring a C++ codebase in Visual Studio 2005. I'm about half way through this process now and I've commented out a lot of old code and replaced or moved it. Now I'm searching to see that I have to change next but the search function keeps bringing me the old commented out stuff I no longer care about. I don't really want to delete that old code yet, just in case.

    Is there any way I can search all files in the solution and get results ignoring what is commented out? I don't see a way in visual studio itself, is the perhaps a plug-in that would do it?