visual-studio 查找和替换 - 添加回车或换行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4336417/
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 12:50:33  来源:igfitidea点击:

Find and replace - Add carriage return OR Newline

visual-studioreplacecarriage-return

提问by Mantisimo

In the case of following string to be parsed.

在以下要解析的字符串的情况下。

ford mustang,10,blue~~?bugatti veyron,13,black

I want to replace the ~~?with a carriage return

我想~~?用一个替换carriage return

Replacing with \njust adds the string "\n"

替换为\n只添加字符串"\n"

How can this be done?

如何才能做到这一点?

回答by Martin

Make sure "Use: Regular expressions" is selected in the Find and Replace dialog:

确保在“查找和替换”对话框中选择“使用:正则表达式”:

Find/Replace Dialog Use Regular expressions

查找/替换对话框使用正则表达式

Note that for Visual Studio 2010, this doesn't work in the Visual Studio Productivity Power Tools' "Quick Find" extension (as of the July 2011 update); instead, you'll need to use the full Find and Replace dialog (use Ctrl+Shift+H, or Edit --> Find and Replace --> Replace in Files), and change the scope to "Current Document".

请注意,对于 Visual Studio 2010,这在Visual Studio Productivity Power Tools的“快速查找”扩展中不起作用(截至 2011 年 7 月更新);相反,您需要使用完整的“查找和替换”对话框(使用 Ctrl+Shift+H,或编辑 --> 查找和替换 --> 在文件中替换),并将范围更改为“当前文档”。

回答by Dean

You can also try \x0d\x0ain the "Replace with" box with "Use regular Expression" box checked to get carriage return + line feed using Visual Studio Find/Replace. Using \n(line feed) is the same as \x0a

您还可以尝试\x0d\x0a在“替换为”框中选中“使用正则表达式”框以使用 Visual Studio 查找/替换获取回车 + 换行符。使用\n(换行)与\x0a

回答by detunized

If you set "Use regular expressions" flag then \n would be translated. But keep in mind that you would have to modify you search term to be regexp friendly. In your case it should be escaped like this "\~\~\?" (no quotes).

如果您设置“使用正则表达式”标志,则 \n 将被翻译。但请记住,您必须将搜索词修改为对正则表达式友好。在你的情况下,它应该像这样转义 "\~\~\?" (没有引号)。

回答by Suzanne Dupéron

If you want to avoid the hassle of escaping the special characters in your search and replacement string when using regular expressions, do the following steps:

如果您想避免在使用正则表达式时转义搜索和替换字符串中的特殊字符的麻烦,请执行以下步骤:

  1. Search for your original string, and replace it with "UniqueString42", with regular expressions off.
  2. Search for "UniqueString42" and replace it with "UniqueString42\nUniqueString1337", with regular expressions on
  3. Search for "UniqueString42" and replace it with the first line of your replacement (often your original string), with regular expressions off.
  4. Search for "UniqueString42" and replace it with the second line of your replacement, with regular expressions off.
  1. 搜索您的原始字符串,并将其替换为“UniqueString42”,并使用正则表达式off
  2. 搜索“UniqueString42”,并以“UniqueString42 \ nUniqueString1337”取代它,用正则表达式
  3. 搜索“UniqueString42”并将其替换为替换的第一行(通常是原始字符串),使用正则表达式off
  4. 搜索“UniqueString42”并将其替换为替换的第二行,使用正则表达式off

Note that even if you want to manually pich matches for the first search and replace, you can safely use "replace all" for the three last steps.

请注意,即使您想为第一次搜索和替换手动挑选匹配项,您也可以安全地对最后三个步骤使用“全部替换”。

Example

例子

For example, if you want to replace this:

例如,如果你想替换这个:

public IFoo SomeField { get { return this.SomeField; } }

with that:

接着就,随即:

public IFoo Foo { get { return this.MyFoo; } }
public IBar Bar { get { return this.MyBar; } }

You would do the following substitutions:

您将进行以下替换:

  1. public IFoo SomeField { get { return this.SomeField; } }XOXOXOXO(regex off).
  2. XOXOXOXOXOXOXOXO\nHUHUHUHU(regex on).
  3. XOXOXOXOpublic IFoo Foo { get { return this.MyFoo; } }(regex off).
  4. HUHUHUHUpublic IFoo Bar { get { return this.MyBar; } }(regex off).
  1. public IFoo SomeField { get { return this.SomeField; } }XOXOXOXO(正则表达式关闭)。
  2. XOXOXOXOXOXOXOXO\nHUHUHUHU(正则表达式)。
  3. XOXOXOXOpublic IFoo Foo { get { return this.MyFoo; } }(正则表达式关闭)。
  4. HUHUHUHUpublic IFoo Bar { get { return this.MyBar; } }(正则表达式关闭)。

回答by Peter Macej

You can use Multiline Search and Replace in Visual Studio macrowhich provides nice GUI for the task.

您可以在 Visual Studio 宏中使用多行搜索和替换,它为任务提供了很好的 GUI。

enter image description here

在此处输入图片说明