You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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\n and 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

  1. Open the Find dialog with Ctrl+F
  2. Click the small arrow next to the "Find Options" label to expand the menu
  3. Check the Use Regular Expressions box (this switches Visual Studio to regex mode)
  4. Paste the regex pattern into the "Find what" field
  5. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:15:02