正则表达式替换可变数量的捕获组 |在 Visual Studio 中进行批量 XML 文档编辑

问题描述 投票:0回答:1

这是一个多合一的问题。

我想将换行符注入到我的 C# 项目的 xml 文档中。
出发地

/// <summary>
/// Event for lazy async var reading. 
/// Contains all vars that partecipated to a certain poll,  
/// indepentently of the fact it succeded or the value changed </summary>

/// <summary>
/// <br>Event for lazy async var reading. </br>
/// <br>Contains all vars that partecipated to a certain poll,</br>  
/// <br>indepentently of the fact it succeded or the value changed </br></summary>

我的第一个方法是运行正则表达式查找和替换,我成功地根据不同的缩进等正确选择了注释。

当涉及到捕获组时,我没有找到一种方法来引用构成身体的线条。
该行的捕获组和内部捕获组似乎都丢失了。

我的正则表达式:

(?x:
    (?(DEFINE)
        (?'line'\/\/\/\s*([^\n]+)\n*)
    )
    (?'head'<summary>(?:\s*\n | \s*[^\n]+\n*))  #select summary tag and skip the first line.
    (?P>line)+                                  #select each line, capturing both the line and a subset of its content
    (?'foot'(?:\/\/\/\s*)*<\/summary>)          #select summary closure and the empty comment-lines preceding it
)

这个表达式产生

{0}:  full match  
{1 (named head)}:   \<summary\>...  
{4}:  last matched (?P>line)  
{5 (named foot)}:   ...\</summary\>  

捕获组 {2} 和 {3} 去了哪里?

(?'line'..)
的内部捕捉团去哪儿了?
是否有 Visual Studio 扩展可以胜任这项任务?

即使正则表达式有效,我也不知道如何引用所有中间组,因为它们的数量是可变的。
我的计划是明确引用所有编号组直到某个编号,并命名其他组以避免选择。有更好的选择吗?

附注如果有一种方法可以发布以颜色编码的漂亮方式格式化的正则表达式“代码”,我没有找到它。欢迎大家指点。

p.p.s。编辑:我显然无法找到相关问题指出“仅通过正则表达式是不可行的,只能通过实际语言中的函数”

regex visual-studio xml-documentation
1个回答
0
投票

这显然可以仅通过正则表达式来完成。
Visual Studio 没有可用的正则表达式引擎。
不过,这可以在 C# 中使用捕获集合来完成。

您也可以硬着头皮使用 Pcre 引擎。
这是给你的一个主意。

(?m)(?:^///\h*<summary>\h*\K(?'line'(?:(?!</?summary\s*>)\S)+(?:\h*(?:(?!</?summary\s*>)\S)+)*)|\G\s*^///\h*\K(?&line))

替补

<br>$0</br>

https://regex101.com/r/MX3c66/1

© www.soinside.com 2019 - 2024. All rights reserved.