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

前端 未结 5 1788
北恋
北恋 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:22

    search for this \s\{ and replace with \n\{

    Your editor needs to support regular expressions in both search and replace fields. If you can't use the \n in the replace dialog because it takes the string literally, try a option-enter followed by {, that works in most editors I tried.

    • the \s is a space character (if there could be more spaces you can use \s+)

    note it has to be \s+ instead of \s* as someone fixed because indeed \s* also matches in case there is no space.

    • the \{ needs the backslash because {
      needs to be escaped as it has another meaning in a regex
    • the \n is for a newline

    The best way however would be to reformat your code where you choose to have your { on a new line. Most editors allow you to set these options.

    Another way is to use a code beautifier, you can google these online and some allow to change settings like that.

提交回复
热议问题