如何在Visual Studio中用正则表达式查找C#连续多行注释
Finding Consecutive Line Comment Blocks in C# (Visual Studio Regex)
Hey there! I’ve got you covered for finding those consecutive line comment blocks in C# using Visual Studio’s regex search—perfect for cleaning up leftover refactoring comments.
The Regex Pattern
Here’s the regex you’ll need to match 2 or more consecutive line comments (no blank lines in between):
(^\s*//.*\r?\n)+\s*//.*
Breakdown of the Pattern
Let’s break down what each part does to help you understand it:
^: Anchors the match to the start of a line (avoids matching//inside string literals like"// not a comment")\s*: Matches any number of leading whitespace characters (handles indentation from tabs or spaces)//.*: Matches the comment itself—from//to the end of the line\r?\n: Matches line breaks (works with both Windows-style\r\nand Unix-style\n)(...)+\s*//.*: Ensures we match at least one full comment line followed by a line break, plus one final comment line—so this catches blocks of 2+ consecutive comments
How to Use This in Visual Studio
- Open the Find dialog with
Ctrl+F - Click the small arrow next to the "Find Options" label to expand the menu
- Check the Use Regular Expressions box (this switches Visual Studio to regex mode)
- Paste the regex pattern into the "Find what" field
- Click Find All—you’ll see every block of consecutive line comments listed in the Results pane
Pro Tips
- If you want to delete these comment blocks, switch to the Replace tab (
Ctrl+H), use the same regex in "Find what", leave "Replace with" empty, and double-check matches before replacing en masse. - This regex won’t match single-line comments (which is exactly what you want, since you only care about consecutive blocks from refactoring).
- It ignores
//inside string literals because of the^anchor—so you don’t have to worry about false positives there.
内容的提问来源于stack exchange,提问作者Phillip Ngan
相关产品推荐
相关产品推荐

