【发布时间】:2017-06-01 11:49:46
【问题描述】:
我正在尝试替换大型项目中的 XML 文档 cmets,该项目错误地使用 <item></item> 而不是 <item><description></description></item>。我认为使用 Visual Studio 的查找和替换功能可能是最快的方法。所以,我想出了以下正则表达式:
(?ixs)(?<=///.*<item\b[^>]*>)(?<description>[^\<]*?)(?=</\s*?item>)
在 Visual Studio 2015 的查找和替换功能的“在文件中查找”模式下,正则表达式匹配项目中的 249 行。
但是,如果我尝试使用“查找和替换”,并在“替换为”文本框中使用 <description>${description}</description>,则会得到零匹配项。
我尝试使用$1、$2 和$+ 而不是${description},但在每种情况下我都得到零匹配。
我还在“替换为”文本框中使用了文字值 REPLACEMENT,它将所有 249 行替换为单词 REPLACEMENT。
我在 Visual Studio 2015 和 2017 中都试过了,结果相同。
根据MSDN,这应该是替换文本的语法。那么为什么它在 Visual Studio 中不起作用?
注意:我正在尝试的项目是 https://github.com/apache/lucenenet,以防有人想在与我相同的条件下进行测试。
环境:
- Windows 10 x64
- Visual Studio 2015 社区版(更新 3)
示例输入
/// For more examples, see the <see cref="Lucene.Net.Analysis"/> namespace documentation.
/// <para/>
/// For some concrete implementations bundled with Lucene, look in the analysis modules:
/// <list type="bullet">
/// <item>Common:
/// Analyzers for indexing content in different languages and domains.</item>
/// <item>ICU:
/// Exposes functionality from ICU to Apache Lucene.</item>
/// <item>Kuromoji:
/// Morphological analyzer for Japanese text.</item>
/// <item>Morfologik:
/// Dictionary-driven lemmatization for the Polish language.</item>
/// <item>Phonetic:
/// Analysis for indexing phonetic signatures (for sounds-alike search).</item>
/// <item>Smart Chinese:
/// Analyzer for Simplified Chinese, which indexes words.</item>
/// <item>Stempel:
/// Algorithmic Stemmer for the Polish Language.</item>
/// <item>UIMA:
/// Analysis integration with Apache UIMA.</item>
【问题讨论】:
-
在将
(?<description>[^\<]*?)更改为([^\<]*?)时,您是否尝试使用$1反向引用? -
@revo - 是的,我也试过了。我还使用自制的正则表达式测试实用程序测试了正则表达式,该实用程序正确地将文本提取到默认和第一个捕获组中。
-
您确认正则表达式匹配字符而不是位置?
-
是的,它从组中提取文本并且绝对正确地匹配它。或者,你的意思是别的吗?
-
好的。另请添加示例输入。
标签: c# regex visual-studio visual-studio-2015