67

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.

2
  • related: [Feature request: 'Ignore comments' checkbox in the 'Find' dialog] github.com/microsoft/vscode/issues/11688 ; still not solved
    – Nor.Z
    Feb 18 at 4:14
  • Hi! You actually never accepted an answer in all the years, so maybe you wanna check in and see if over ten years later your question could finally be answered ;-)
    – Pingui
    Feb 22 at 18:42

4 Answers 4

53

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

2
  • There was a space in front of the expression. Took me a while to figure out why it wasn't working for me. Thanks. Jan 16, 2018 at 16:12
  • It still matches lines starting with * as for instance jsdoc comments do.
    – Magne
    Apr 5, 2022 at 12:36
25

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.

4
  • Very cool. I don't think that it will handle block comments (/* ... */) but I really like how the regex is broken down.
    – A.R.
    Jul 9, 2012 at 12:55
  • 11
    Broken for me! According to docs the syntax changed: ^(?![ \t]*//).*your_search_term. Docs: msdn.microsoft.com/query/… Nov 29, 2013 at 20:44
  • 4
    @JaredThirsk I suggest you to add an answer ;).
    – shA.t
    Aug 12, 2015 at 7:11
  • @JaredThirsk broken link
    – LZH
    May 25, 2016 at 9:08
0

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.

1
  • 2
    yeah, don't forget anything between /* .. */ too.
    – A.R.
    Jul 3, 2012 at 17:15
0

You probably mean any comments, both single- (//) and MULTI-LINE (/**/).

The other answers only deal with single-line-comments. So here comes my VS Code-compatible Regex for both of them at the same time:

(?<!//.*)(?!(/\*).*)YOUR_SEARCH_TERM(?![^/\*]*?\*/)

Explanation:

(?<!//.*) is a negative look-behind against single-line-comments (//)

(?!(/\*).*) invalidates your search term if it is preceded by /*

(?![^/\*]*?\*/) invalidates your search term if it is followed by */ (short of a /* between your search term and itself, because then your search term would be outside of /**/ and you want it to be matched again).

To use it in VS Code, just type Ctrl + F and tick the regex-icon in the search-field popping up (with void being your exemplary search term):

enter image description here

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.