Visual Studio 2015正则表达式替换不起作用(正则表达式匹配工作)

时间:2021-08-26 16:52:26

I am attempting to replace XML documentation comments in a large project that have been incorrectly using <item></item> instead of <item><description></description></item>. I figured that using the Visual Studio find and replace functionality would probably be the quickest way to do this. So, I came up with the following regex:

我试图在错误地使用 而不是 的大型项目中替换XML文档注释。我认为使用Visual Studio查找和替换功能可能是最快的方法。所以,我想出了以下正则表达式:

(?ixs)(?<=///.*<item\b[^>]*>)(?<description>[^\<]*?)(?=</\s*?item>)

In the "Find In Files" mode in Visual Studio 2015's Find and Replace functionality, the regex matches 249 lines in the project.

在Visual Studio 2015的“查找和替换”功能中的“在文件中查找”模式中,正则表达式匹配项目中的249行。

However, if I try to use "Find and Replace", and use <description>${description}</description> in the "Replace with" textbox, I get zero matches.

但是,如果我尝试使用“查找和替换”,并在“替换为”文本框中使用 $ {description} ,则会得到零匹配。

I tried using $1, $2, and $+ instead of ${description}, but in each case I get zero matches.

我尝试使用$ 1,$ 2和$ +而不是$ {description},但在每种情况下我得到零匹配。

I also used a literal value REPLACEMENT in the "Replace with" textbox and it replaces all 249 lines with the word REPLACEMENT.

我还在“替换为”文本框中使用了文字值REPLACEMENT,它用替换单词REPLACEMENT替换了所有249行。

I tried this both in Visual Studio 2015 and 2017 with the same result.

我在Visual Studio 2015和2017中都尝试了相同的结果。

According to MSDN, this should be the syntax for substituting the text. So why is it not working in Visual Studio?

根据MSDN,这应该是替换文本的语法。那为什么它在Visual Studio中不起作用?

NOTE: The project I am trying this with is https://github.com/apache/lucenenet, in case anyone wants to test under the same conditions I am.

注意:我正在尝试使用的项目是https://github.com/apache/lucenenet,以防有人想要在相同的条件下进行测试。

Environment:

  • Windows 10 x64
  • Windows 10 x64

  • Visual Studio 2015 Community Edition (Update 3)
  • Visual Studio 2015社区版(更新3)

Sample Input

/// 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>

2 个解决方案

#1


2  

I can confirm the problem in VS2012. Had to change regex to account for newlines (([^\<]|\r\n)*? instead of just [^\<]*?). It works when replacing items one by one, but does nothing when you want to replace all. Could be a bug.

我可以在VS2012中确认问题。不得不改变正则表达式以换取换行符(([^ \ <] | \ r \ n)*?而不仅仅是[^ \ <] *?)。它在逐个替换项目时起作用,但在您想要替换所有项目时什么也不做。可能是一个错误。

This works though (not using lookaheads and lookbehinds):

这虽然有效(不使用lookaheads和lookbehinds):

Find: <item>(([^\<]|\r\n)*)
Replace with: <item><description>$1</description>

查找: (([^ \ <] | \ r \ n)*)替换为: $ 1

#2


0  

I have also encountered this issue (Find All and Replace All not working but Replace working in a given file) with the regular Find & Replace in Visual Studio 2015. I was performing a find in "Selection": I selected the text I wanted to run the REGEX replace over, pressed Ctrl + H for the Find & Replace tool and started typing in the REGEX (at this point Visual Studio started highlighting text within the selection). When I then clicked "Replace All" or "Find All" no results were found but I found out that if I selected the text again with the Find & Replace tool still open then Find All and Replace All started working again!

我在Visual Studio 2015中使用常规的查找和替换也遇到了这个问题(查找全部并且全部替换不起作用但替换在给定文件中工作)。我在“选择”中执行查找:我选择了我想要的文本运行REGEX替换,按Ctrl + H查找和替换工具并开始在REGEX中键入(此时Visual Studio开始突出显示选择内的文本)。当我单击“全部替换”或“全部查找”时,未找到任何结果,但我发现如果我再次选择文本并且仍然打开“查找和替换”工具,则“查找全部”和“全部替换”再次开始工作!

This seems like a bug as the text was already selected and became deselected as I was writing my REGEX (even though it no longer had the usual selection background colour, it did still appear to have a background highlight colour when compared to the rest of the file to indicate that it was still selected even though Find & Replace acted as though it wasn't selected!).

这似乎是一个错误,因为文本已被选中并在我编写REGEX时被取消选择(即使它不再具有通常的选择背景颜色,与其余部分相比,它仍然看起来仍然具有背景突出显示颜色。文件,表明它仍然被选中,即使查找和替换的行为好像没有被选中!)。

I don't know whether this answers your question directly as I'm not sure what mode you were using for "Find In", but if it was "Selection" then this could be the issue.

我不知道这是否直接回答了你的问题,因为我不确定你在“查找”中使用了什么模式,但如果它是“选择”那么这可能是问题。

#1


2  

I can confirm the problem in VS2012. Had to change regex to account for newlines (([^\<]|\r\n)*? instead of just [^\<]*?). It works when replacing items one by one, but does nothing when you want to replace all. Could be a bug.

我可以在VS2012中确认问题。不得不改变正则表达式以换取换行符(([^ \ <] | \ r \ n)*?而不仅仅是[^ \ <] *?)。它在逐个替换项目时起作用,但在您想要替换所有项目时什么也不做。可能是一个错误。

This works though (not using lookaheads and lookbehinds):

这虽然有效(不使用lookaheads和lookbehinds):

Find: <item>(([^\<]|\r\n)*)
Replace with: <item><description>$1</description>

查找: (([^ \ <] | \ r \ n)*)替换为: $ 1

#2


0  

I have also encountered this issue (Find All and Replace All not working but Replace working in a given file) with the regular Find & Replace in Visual Studio 2015. I was performing a find in "Selection": I selected the text I wanted to run the REGEX replace over, pressed Ctrl + H for the Find & Replace tool and started typing in the REGEX (at this point Visual Studio started highlighting text within the selection). When I then clicked "Replace All" or "Find All" no results were found but I found out that if I selected the text again with the Find & Replace tool still open then Find All and Replace All started working again!

我在Visual Studio 2015中使用常规的查找和替换也遇到了这个问题(查找全部并且全部替换不起作用但替换在给定文件中工作)。我在“选择”中执行查找:我选择了我想要的文本运行REGEX替换,按Ctrl + H查找和替换工具并开始在REGEX中键入(此时Visual Studio开始突出显示选择内的文本)。当我单击“全部替换”或“全部查找”时,未找到任何结果,但我发现如果我再次选择文本并且仍然打开“查找和替换”工具,则“查找全部”和“全部替换”再次开始工作!

This seems like a bug as the text was already selected and became deselected as I was writing my REGEX (even though it no longer had the usual selection background colour, it did still appear to have a background highlight colour when compared to the rest of the file to indicate that it was still selected even though Find & Replace acted as though it wasn't selected!).

这似乎是一个错误,因为文本已被选中并在我编写REGEX时被取消选择(即使它不再具有通常的选择背景颜色,与其余部分相比,它仍然看起来仍然具有背景突出显示颜色。文件,表明它仍然被选中,即使查找和替换的行为好像没有被选中!)。

I don't know whether this answers your question directly as I'm not sure what mode you were using for "Find In", but if it was "Selection" then this could be the issue.

我不知道这是否直接回答了你的问题,因为我不确定你在“查找”中使用了什么模式,但如果它是“选择”那么这可能是问题。