Aaron Aaron - 2 months ago 13x
Objective-C Question

Regular Expression to replace " {" with "(newline){" in xcode

I need to change my coding style of putting opening braces in same line to new line. I need to find and replace the (space){ with (newline){. I heard using regular expression find and replace, its pretty simple.

Could anyone help me on this?


You could try the following:

  • In the Find box, type space \ { $
  • In the Replace box, type control+q return {

control+q is needed to quote the return key. There’s no visual feedback for typing control+q return, so the only visible character in the replace box is the opening curly brace:

Screenshot Find & Replace

Although this answers your question, there’s (at least!) one problem: it won’t indent the opening curly brace, so something like

- (void)method {
    for (obj in collection) {
        NSLog(@"%@", obj);

is converted to

- (void)method
    for (obj in collection)
        NSLog(@"%@", obj);

The menu item Edit > Format > Re-Indent will place the opening curly braces in the correct indentation tab but there might be non-desired side effects to your code style.

Edit: as commented in the other answer, you might want a regular expression that matches an arbitrary number of whitespaces surrounding the curly brace, e.g. \s*{\s*$