0

When using the Find command in Visual Studio, what is a regex that can be used to find blocks of comments in C# code (i.e. comments on consecutive lines) e.g.

// No need to find single line comment
void Foo()
{
    // I want to find this line.
    // And this line, because they are
    // on consecutive lines
}

I sometimes will rework code by commenting out the original version, work on a copy, and then later on coming back and to delete the original commented out code. I'm looking for a regex that will help me to locate the commented out code.

2
  • 3
    This is a kind of roll-your-own source control? The better thing to do would be to add //TODO comments, which show up in the Task List. You can even make custom task tokens (see link I provided). Commented Apr 5, 2018 at 20:31
  • Good idea to use TODO. I use my process built over years of using other editors. Commented Apr 5, 2018 at 20:50

2 Answers 2

2

Here's a starting point. This obviously misses /* comment */ comments.

//.*\r?\n.*//

//               match // literally
.*               match any character 0 to unlimited times
\r?              match a carriage return optionally
\n               match a new line
.*               match any character 0 to unlimited times
//               match // literally 

If you flag comments with TODO, i.e. //TODO: Fix this broken thing, you can easily find them in the task list later (View menu, Task List).

Sign up to request clarification or add additional context in comments.

Comments

2

This will do it [\s\S]([^a-zA-Z/][^\n\r][a-zA-Z/]?//.*[/]*)

See https://regex101.com/r/wsGNZy/1

1 Comment

Finds each line seperately, instead of all comment lines en bloc.

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.