Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude comments when searching in Visual Studio

Is there a way to perform searches (Find / Find in Files) in visual studio that will exclude matches in comments? While sometimes it is useful, other times it is the opposite. For all of the options presented, I figured it would be in there, but I can't find it if it is.

I am using VS 2010/2012 about equally by the way.

like image 417
A.R. Avatar asked Jul 03 '12 15:07

A.R.


People also ask

How do I get rid of comments in Visual Studio?

Ctrl+K+U will uncomment the code.

How exclude files from VS code search?

To exclude a folder, go to File > Preferences, and search for file. exclude in the search settings. You can add the pattern of the folder you don't want Visual Studio Code to open.

How do I find commented codes in Visual Studio?

Visual Studio 17.2 Preview 3 introduces a brand-new All-In-One search experience that merges the existing VS Search (Ctrl + Q) and Go To (Ctrl + T) to allow you to search both your code and Visual Studio features quicker and easier than ever, all in the same place.


3 Answers

Here's the regex that works for me for newer versions of Visual Studio:

^(?![ \t]*//).*your_search_term

Note that the syntax changed as of VS 2012:

Visual Studio 2012 uses .NET Framework regular expressions to find and replace text. In Visual Studio 2010 and earlier versions, Visual Studio used custom regular expression syntax in the Find and Replace windows.

Reference: https://msdn.microsoft.com/en-us/library/vstudio/2k3te2cs(v=vs.110).aspx

like image 61
Jared Thirsk Avatar answered Oct 13 '22 05:10

Jared Thirsk


you could try the regex as below:

^~(: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 :-)

saw this at another post.

like image 37
LZH Avatar answered Oct 13 '22 04:10

LZH


I don't believe it's an option in VS. You could try regular expressions, but those are limited by how creative you can be. It seems like it would be not entirely difficult to search for lines not beginning with // using a regex.

like image 1
David Mason Avatar answered Oct 13 '22 04:10

David Mason