简体   繁体   中英

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 :

if (a > 5)
{
}

to the K&R style :

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). 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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