Regular Expression to replace “ {” with “(newline){” in xcode

前端 未结 5 1792
北恋
北恋 2021-02-02 08:02

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 fi

5条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-02 08:36

    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*$

提交回复
热议问题