9

I am beginning work on a large Visual Studio solution, and have come across areas that have large blocks of commented-out code. The blocks are usually more than 3 lines in length, and all start with // (probably Ctrl+K,C was used to comment these out). I would like to try to find out all the places these exist in my solution so our team can address them, and delete them if they are no longer needed (after all, we are using proper source-code control and can always go back in history to see the old code!).

So I am interested in commented out blocks of code that span more than 3 consecutive lines (and I don't want get a lot of false-positives for the XML documentation comments either). What regular expression can I use in Visual Studio's find dialog to look for these blocks? I tried the following, but it doesn't seem to work:

//[^/].*\n[:Wh]*//[^/].*\n[:Wh]*//[^/]

I'm not trying to do a find-and-replace - just find them and we'll look at them before deleting the code if it's no longer needed.

Thanks!

2
  • Could you post your regex as code (indent it with four spaces)? Now the *'s are getting "eaten" as italic tags. Commented Oct 14, 2009 at 13:40
  • +1 For pointing out that old code should be deleted, not commented. Like you said, version control system can get you that code back if needed. Commented Oct 14, 2009 at 15:20

3 Answers 3

8

The other answers didn't work for me in Visual Studio 2012 (not sure if anything's changed). The following did:

(^[ \t]*//[^/].*\r?\n){3,}
Sign up to request clarification or add additional context in comments.

Comments

5

Try this one:

[:Wh]//[^/].*\n[:Wh]*//[^/].*\n[:Wh]*//[^/].*\n

Works for me.

1 Comment

Thanks. Modified for python and ignoring TODO comments: (\\s*#[^#](?!TODO).*\\n\\s*#[^#](?!TODO).*\\n). Escaped slashes for use in a VS Code extension named Highlight.
1

Try this one on for size:

((^.*[^/])|(^)//[^/].*\n)+

Actually this works best for me so far:

(((^.*[^/])|(^)//[^/].*\n)|(^//\n))(((^.*[^/])|(^)//[^/].*\n)|
(^//\n))(((^.*[^/])|(^)//[^/].*\n)|(^//\n))(((^.*[^/])|(^)//[^/].*\n)|(^//\n))+

It is long but it only gets four or more continuous comment lines and handles blank comment lines (just // and nothing else no content).

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.