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

后端 未结 8 531
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-02 09:20

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\'

相关标签:
8条回答
  • 2021-02-02 09:45

    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.*.

    0 讨论(0)
  • 2021-02-02 09:48

    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.

    0 讨论(0)
  • 2021-02-02 09:50

    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.

    0 讨论(0)
  • 2021-02-02 09:56

    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

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

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

     ^(?![ \t]*//).*mysearchterm
    
    0 讨论(0)
  • 2021-02-02 09:59

    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

    0 讨论(0)
  • 2021-02-02 09:59

    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:

    ^~(.*//)~(.*/\*)~(.*\<\/)~(.*/\>).*/
    
    0 讨论(0)
提交回复
热议问题