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

StackOverflow https://stackoverflow.com/questions/17616085

Frage

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) {
}

?

War es hilfreich?

Lösung

Search for \n[ \t]*\{\n and replace with {\n or with {\u000D\u000A 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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top