visual-studio Visual Studio 中的多行查找和替换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2273434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 11:33:00  来源:igfitidea点击:

Multiline Find & Replace in Visual Studio

visual-studioidereplacemultiline

提问by hawbsl

Can it be done? We're using VS2005 and VS2008 and VS2010.

可以做到吗?我们使用的是 VS2005、VS2008 和 VS2010。

I don't mean regular expressions - which have their place - but plain old text find & replace. I know we can do it (at a pinch) with regular expressions using the \n tag but prefer not to get tangled up in regex escape characters, plus there's a readability issue.

我的意思不是正则表达式——它们有它们的位置——而是简单的旧文本查找和替换。我知道我们可以(在紧要关头)使用 \n 标签使用正则表达式来完成,但不想被正则表达式转义字符纠缠在一起,另外还有一个可读性问题。

If it can't be done what plain and simple (free) alternative are people using? That doesn't involve knocking up our own macro.

如果无法完成,人们使用什么简单而简单(免费)的替代方法?这不涉及敲开我们自己的宏。

回答by SV.

I finally found it..

我终于找到了。。

No need to download and load any external macro.

无需下载和加载任何外部宏。

Its working in Visual Studio 2008 with in-built macro at least. :)

它至少可以在带有内置宏的 Visual Studio 2008 中工作。:)

Steps :
1) Select text you want to find.
2) Press "Alt+F8" or open "Tools -> Macros -> Macro Explorer"
3) Double click "Sample -> Utilities -> FindLine". (It will open Find box with your selection loaded in "Find" field. Don't worry about truncated text shown in "Find" field. Trust me, field has it all..MS way of showing it may be... :) )
4) Click on "Quick Replace" button on "Find And Replace" dialog box. Enter your replace with text.
5) And Click any of 3 buttons as per your requirement...and its done. :)

步骤:
1) 选择要查找的文本。
2) 按“Alt+F8”或打开“工具 -> 宏 -> 宏资源管理器”
3) 双击“示例 -> 实用程序 -> FindLine”。(它将打开“查找”框,在“查找”字段中加载您的选择。不要担心“查找”字段中显示的截断文本。相信我,字段拥有一切..显示它的 MS 方式可能是......: ) )
4) 单击“查找和替换”对话框中的“快速替换”按钮。输入您的替换文本。
5) 然后根据您的要求单击 3 个按钮中的任何一个...就完成了。:)

Hurray.. its working.. May not be a straight forward way to do it but you know with MS.. nothing is straightforward and easy.. :)

万岁.. 它的工作.. 可能不是一个直接的方法来做到这一点,但你知道与 MS.. 没有什么是直接和容易的.. :)

回答by Robert Adams

You might want to take a look at this blog entry:

你可能想看看这个博客条目:

Multiline Search and Replace in Visual Studio

Visual Studio 中的多行搜索和替换

Provides macro code to escape your search and show the VS find and replace dialog.

提供宏代码以逃避您的搜索并显示 VS 查找和替换对话框。

回答by woohoo

This works today in Visual Studio 2012:

这今天在 Visual Studio 2012 中有效:

fooPatternToStart.*(.*\n)+?.*barPatternToEnd

See how the (.*\n)+?part does the match across multiple lines, non-greedy.
fooPatternToStartis some regex pattern on your start line, while barPatternToEndis your pattern to find on another line below, possibly many lines below...

查看部件如何(.*\n)+?跨多行进行匹配,非贪婪。
fooPatternToStart是你的起始行上的一些正则表达式模式,而barPatternToEnd你的模式是在下面的另一行上找到的,可能是下面的许多行......

Example found here.

示例在此处找到。

Simple and effective :)

简单有效:)

Note: beforeVS2012, the pattern that worked was: fooPatternToStart.(.\n)+@.*barPatternToEnd

注意:VS2012之前,有效的模式是:fooPatternToStart.(.\n)+@.*barPatternToEnd

回答by Phillip Ngan

You can search for multiline expressions by clicking on the "Use Regular Expressions" checkbox in the "Find and Replace" dialog. Line breaks are then indicated by \n.

您可以通过单击“查找和替换”对话框中的“使用正则表达式”复选框来搜索多行表达式。然后换行用 表示\n

enter image description here

在此处输入图片说明

回答by Mahesh Malpani

Its provided by microsoft only. Please check https://visualstudiogallery.msdn.microsoft.com/699CE302-B0D4-4083-BE0E-1682E873CEBF.

它仅由微软提供。请检查https://visualstudiogallery.msdn.microsoft.com/699CE302-B0D4-4083-BE0E-1682E873CEBF

It uses regular expression only. But for those who don't know regex better to use it.

它仅使用正则表达式。但是对于那些不了解正则表达式的人来说,最好使用它。

回答by zaengi

Regarding the comment of Andrew Corkery Aug 19 '11 at 9:52 above:

关于 Andrew Corkery 于 2011 年 8 月 19 日 9:52 发表的评论:

If you like to specify a multi-line replacement string as well, edit the macro code and set the replacement text as shown below. This will allow you to "fine-tune" your replacement with just the small modifications needed.

如果您还想指定多行替换字符串,请编辑宏代码并设置替换文本,如下所示。这将允许您通过所需的小修改来“微调”您的替换。

 Sub FindLine()
    Dim textSelection As TextSelection

    textSelection = DTE.ActiveDocument.Selection
    textSelection.CharLeft(True)
    DTE.ExecuteCommand("Edit.Find")
    DTE.Find.FindWhat = textSelection.Text

    'also preset replacement text with current selection
    DTE.Find.ReplaceWith = textSelection.Text
End Sub

回答by Fedor Steeman

You could also open the files with UltraEditwhich fully supports MultiLine replace. You can use the trial version if you only intend to use it once.

您也可以使用完全支持多行替换的UltraEdit打开文件。如果您只打算使用一次,则可以使用试用版。

回答by sliderhouserules

The latest version (as of this posting) of Notepad++ does multi-line find/replace. I don't know if this was true back when this was asked. But with no macro support in Visual Studio anymore, this is relevant now.

Notepad++ 的最新版本(截至本帖)执行多行查找/替换。我不知道这在被问到时是否属实。但是由于 Visual Studio 中不再支持宏,这现在很重要。