visual-studio Visual Studio 2008:查找并替换为换行符?

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

Visual Studio 2008: Find and Replace with new line character?

visual-studiovisual-studio-2008

提问by burnt1ce

Sometimes i would like to search for text containing a new line character and there are other times i would like to replace text with a new line character.

有时我想搜索包含换行符的文本,有时我想用换行符替换文本。

How can i do this with visual studio 2008?

我怎样才能用 Visual Studio 2008 做到这一点?

回答by Brian Schmitt

Use a RegEx search:

使用正则表达式搜索:

In the Find Dialog - Expand "Find Options" Check the box for Use: Regular Expressions

在“查找”对话框中 - 展开“查找选项”选中“使用:正则表达式”框

Next to the search box there is now an arrow that is active, it will show you available RegEx options/values.

搜索框旁边现在有一个处于活动状态的箭头,它将显示可用的 RegEx 选项/值。

The value you want will be \n. So "SearchValue\n" should do it.

您想要的值将是\n。所以 "SearchValue\n" 应该这样做。

Be aware that that its not a standard RegEx that you use, it's VS specific. Replace can also use the RegEx values.

请注意,它不是您使用的标准 RegEx,而是 VS 特定的。替换也可以使用 RegEx 值。

回答by MTS

Adding on to Brian Schmitt's answer...

添加到布赖恩施密特的答案......

Regular expression searches using \n work as expected. However you have to be a little careful when using \n in regex replaceswith Visual Studio 2008. For example, if you search for \nand replace with \n(yes, the exact same thing) all of the line breaks in your file(s) will be converted to Unix-style newlines (LF). This may be a bug in Visual Studio. I find it hard to believe this is the intended functionality.

使用 \n 的正则表达式搜索按预期工作。但是,在 regex 中使用 \n替换Visual Studio 2008时必须小心。例如,如果您搜索\n并替换为\n(是的,完全相同)文件中的所有换行符(s) 将被转换为 Unix 风格的换行符 (LF)。这可能是 Visual Studio 中的错误。我发现很难相信这是预期的功能。

To get around this, you can use tagged expressions, using curly braces: e.g. search for SearchValue{\n}and replace with ReplaceValue\1. This ensures that the same line-break character(s) that were found when searching will also be used when replacing.

为了解决这个问题,您可以使用标记表达式,使用花括号:例如搜索SearchValue{\n​​}并替换为ReplaceValue\1。这确保在替换时也将使用搜索时找到的相同换行符。

回答by Peter Macej