删除#region

发布于 2024-10-18 20:39:40 字数 131 浏览 2 评论 0 原文

我不得不接手ac#项目。最初开发该软件的人非常喜欢 #region,因为他用区域包装了所有内容。 这让我几乎发疯,我正在寻找一个工具或插件来从项目中删除所有 #region 。周围有什么东西吗?

I had to take over a c# project. The guy who developed the software in the first place was deeply in love with #region because he wrapped everything with regions.
It makes me almost crazy and I was looking for a tool or addon to remove all #region from the project. Is there something around?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

可是我不能没有你 2024-10-25 20:39:40

只需使用 Visual Studio 内置的“查找和替换”(或“在文件中替换”),您可以通过按 Ctrl + Shift + HH 打开它kbd>)。

要删除#region,您需要启用正则表达式匹配;在“在文件中替换”对话框中,选中“使用:正则表达式”。然后,使用以下模式:“\#region .*\n”,用 "" (空字符串)替换匹配项。

要删除 #endregion,请执行相同的操作,但使用“\#endregion .*\n”作为模式。正则表达式可能对于#endregion来说太过分了,但这并没有什么坏处(以防以前的开发人员在与#endregion<相同的行上留下注释) /code> 或其他)。


注意:其他人发布的模式也应该适合您,它们与我的稍有不同,但您已经了解了总体思路。

Just use Visual Studio's built-in "Find and Replace" (or "Replace in Files", which you can open by pressing Ctrl + Shift + H).

To remove #region, you'll need to enable Regular Expression matching; in the "Replace In Files" dialog, check "Use: Regular Expressions". Then, use the following pattern: "\#region .*\n", replacing matches with "" (the empty string).

To remove #endregion, do the same, but use "\#endregion .*\n" as your pattern. Regular Expressions might be overkill for #endregion, but it wouldn't hurt (in case the previous developer ever left comments on the same line as an #endregion or something).


Note: Others have posted patterns that should work for you as well, they're slightly different than mine but you get the general idea.

慢慢从新开始 2024-10-25 20:39:40

使用一个正则表达式 ^[ \t]*\#[ \t]*(region|endregion).*\n 查找区域和结束区域。替换为空字符串后,带前导空格的整行将被删除。

[ \t]* - 查找前导空格

\#[ \t]*(region|endregion) - 查找 #region 或 #endregion (以及非常罕见的带有空格的情况#) .*\n 之后

- 查找 #region 或 #endregion 之后的所有内容(但在同一行中)

编辑:答案更改为与旧的 Visual Studio 正则表达式语法兼容。是: ^[ \t]*\#(end)?region.*\n (问号不适用于旧语法)

编辑 2:添加 # 之后的 [ \t]* 处理 @Volkiith 发现的非常罕见的情况

Use one regex ^[ \t]*\#[ \t]*(region|endregion).*\n to find both: region and endregion. After replacing by empty string, the whole line with leading spaces will be removed.

[ \t]* - finds leading spaces

\#[ \t]*(region|endregion) - finds #region or #endregion (and also very rare case with spaces after #)

.*\n - finds everything after #region or #endregion (but in the same line)

EDIT: Answer changed to be compatible with old Visual Studio regex syntax. Was: ^[ \t]*\#(end)?region.*\n (question marks do not work for old syntax)

EDIT 2: Added [ \t]* after # to handle very rare case found by @Volkirith

浅语花开 2024-10-25 20:39:40

如果您必须与区域爱好者合作(并保持区域不变),那么我会推荐 “我讨厌#Regions” Visual Studio 扩展。它使区域变得可以忍受 - 默认情况下所有区域都会扩展,并且 #region 指令会以非常小的字体呈现。

Should you have to cooperate with region lovers (and keep regions untouched ), then I would recommend "I hate #Regions" Visual Studio extension. It makes regions tolerable - all regions are expanded by default and #region directives are rendered with very small font.

寂寞陪衬 2024-10-25 20:39:40

在“查找和替换”中,使用 {[#] 作为查找内容: 并将其替换为空字符串。
#EndRegion 很容易替换。

In Find and Replace use {[#]<region[^]*} for Find what: and replace it with empty string.
#EndRegion is simple enough to replace.

长发绾君心 2024-10-25 20:39:40

对于任何使用 ReSharper 的人来说,只需在区域线上输入一个简单的 Atr-Enter 即可。然后,您可以选择删除文件、项目或解决方案中的区域。

有关 JetBrains 的更多信息

For anyone using ReSharper it's just a simple Atr-Enter on the region line. You will then have the option to remove regions in file, in project, or in solution.

More info on JetBrains.

守望孤独 2024-10-25 20:39:40

要删除 #region 后带有换行符,请将以下内容替换为空字符串:

^(?([^\r\n])\s)*\#region\ ([^\r\n])*\r?\n(?([^\r\n])\s)*\r?\n

要将 #endregion 替换为前导空行,请将以下内容替换为空字符串:

^(?([^\r\n])\s)*\r?\n(?([^\r\n])\s)*\#endregion([^\r\n])*\r?\n

To remove #region with a newline after it, replace following with empty string:

^(?([^\r\n])\s)*\#region\ ([^\r\n])*\r?\n(?([^\r\n])\s)*\r?\n

To replace #endregion with a leading empty line, replace following with an empty string:

^(?([^\r\n])\s)*\r?\n(?([^\r\n])\s)*\#endregion([^\r\n])*\r?\n
我只土不豪 2024-10-25 20:39:40

为它编写自己的程序,以递归方式替换 basePath 中所有 *.cs 文件中没有任何内容的区域怎么样?
(提示:如果不是 UTF8,请小心读取文件。)

public static void StripRegions(string fileName, System.Text.RegularExpressions.Regex re)
{
    string input = System.IO.File.ReadAllText(fileName, System.Text.Encoding.UTF8);
    string output = re.Replace(input, "");
    System.IO.File.WriteAllText(fileName, output, System.Text.Encoding.UTF8);
}


public static void StripRegions(string basePath)
{
    System.Text.RegularExpressions.Regex re = new System.Text.RegularExpressions.Regex(@"(^[ \t]*\#[ \t]*(region|endregion).*)(\r)?\n", System.Text.RegularExpressions.RegexOptions.Multiline);

    foreach (string file in System.IO.Directory.GetFiles(basePath, "*.cs", System.IO.SearchOption.AllDirectories))
    {
        StripRegions(file, re);
    }
}

用法:

StripRegions(@"C:\sources\TestProject")

How about writing your own program for it, to replace regions with nothing in all *.cs files in basePath recursively ?
(Hint: Careful with reading files as UTF8 if they aren't.)

public static void StripRegions(string fileName, System.Text.RegularExpressions.Regex re)
{
    string input = System.IO.File.ReadAllText(fileName, System.Text.Encoding.UTF8);
    string output = re.Replace(input, "");
    System.IO.File.WriteAllText(fileName, output, System.Text.Encoding.UTF8);
}


public static void StripRegions(string basePath)
{
    System.Text.RegularExpressions.Regex re = new System.Text.RegularExpressions.Regex(@"(^[ \t]*\#[ \t]*(region|endregion).*)(\r)?\n", System.Text.RegularExpressions.RegexOptions.Multiline);

    foreach (string file in System.IO.Directory.GetFiles(basePath, "*.cs", System.IO.SearchOption.AllDirectories))
    {
        StripRegions(file, re);
    }
}

Usage:

StripRegions(@"C:\sources\TestProject")
节枝 2024-10-25 20:39:40

您可以使用通配符查找/替换:

*\#region *
*\#endregion

并替换为无值。 (注意 # 需要转义,因为 Visual Stuido 使用它来匹配“任意数字”)

You can use the wildcard find/replace:

*\#region *
*\#endregion

And replace with no value. (Note the # needs to be escaped, as visual stuido uses it to match "any number")

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文