visual-studio 如何在 Visual Studio 中搜索并让它忽略注释掉的内容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2153876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 11:28:29  来源:igfitidea点击:

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

visual-studiosearch

提问by RD.

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.

我正在 Visual Studio 2005 中重构 C++ 代码库。我现在已经完成了这个过程的一半,我已经注释掉了很多旧代码并替换或移动了它。现在我正在搜索,看看我接下来必须改变,但搜索功能不断给我带来我不再关心的旧的注释掉的东西。我真的不想删除旧代码,以防万一。

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?

有什么方法可以搜索解决方案中的所有文件并获得结果而忽略注释掉的内容?我在visual studio本身中看不到一种方法,也许是一个可以做到这一点的插件?

采纳答案by jk.

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

删除注释掉的代码,它在源代码管理中吗?也无需将其保存在文件中。

回答by rboehme

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

由于其他提供的解决方案对我不起作用,我终于发现了以下解决方案:

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

^~(: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_termyour search term :-)
  • ^从行首
  • ~(不是以下
  • :b*任意数量的空格,后跟
  • //评论开始
  • )非结束
  • .*任何字符都可能出现在前面
  • 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

您必须单击查找窗口上的“使用正则表达式”按钮(点和星号)才能应用正则表达式搜索

回答by MaR

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.

或者甚至更好:视觉辅助和它的彩色“查找所有引用”和“转到”映射到方便的快捷方式。这极大地加快了导航速度。

回答by MaR

If you comment your old code with //you can use regular expressions while searching for something in your codebase. Something like this for example: ^[^/][^/].*your_function_name.*.

如果您//使用正则表达式注释旧代码,则可以在代码库中搜索某些内容时使用正则表达式。像这样的东西,例如:^[^/][^/].*your_function_name.*

回答by Juancho

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:

因此,取代了:b*.*,并且增加了<>所以只有整个单词被发现,再经过一些较旧的C风格的注释,其中有一个去了/*就行了:

^~(.*//)~(.*/\*).*<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.

在我的情况下,我正在寻找所有实例new,不适合重构帮助,以及大量的误报。我也没有想出如何避免引用字符串中的匹配。

回答by spinalfrontier

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:

我想补充的,因为我在做一个“查找所有”在代码中使用除法运算,用下面的排除意见以及<//>来自ASPX文件:

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

回答by user2728841

In Visual Basic within Visual Studio 2015, I was able to search for text outside of comments by adapting glassiko's comment from the most upvoted answer

在 Visual Studio 2015 中的 Visual Basic 中,我能够通过从最受好评的答案中调整 glassiko 的评论来搜索评论之外的文本

^(?![ \t]*[']).*mysearchterm

And in C# you would use glassiko's comment exactly as it was

在 C# 中,您将完全按照原样使用 glassiko 的注释

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

回答by Evgeniy Vasilyev

Better use \sI think. ^(?![\s]*//).*your_search_term

我认为最好使用\s^(?![\s]*//).*your_search_term