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

문제

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 {\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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top