简体   繁体   English

RegEx:从ANSI C ++样式的大括号(换行)切换为K&R样式(与声明相同的行)

[英]RegEx: Switch from ANSI C++ style open braces (new line) to K&R style (same line as the statement)

How would you write a regular expression for match and replace in order to reformat C++ code from the ANSI C++ style : 您如何编写用于match和replace的正则表达式,以便从ANSI C ++样式重新格式化C ++代码:

if (a > 5)
{
}

to the K&R style : K&R风格

if (a > 5) {
}

?

Search for \\n[ \\t]*\\{\\n and replace with {\\n or with {\ \ if you want to preserve the Windows-style line endings (CR+LF). 搜索\\n[ \\t]*\\{\\n ,如果要保留Windows样式的行尾(CR + LF),则替换为{\\n{\ \ Note the space in front of the brace for the replace pattern. 请注意在括号前面的替换模式的空间。

Explanation: match a new line followed by series of spaces and/or tabs, an open brace and another new line. 说明:匹配一个新行,然后是一系列空格和/或制表符,一个大括号和另一个新行。 Replace with a space, open brace and new line. 用空格代替,打开大括号并换行。

Worked with "Quick Replace" in Visual Studio 2010. 在Visual Studio 2010中使用“快速替换”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM