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

前端 未结 5 1784
北恋
北恋 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.

    0 讨论(0)
  • 2021-02-02 08:22

    i searched a while to find out, that you have to enable the find options for regular expressions first on the small magnifier glass in the left side of the find-input field ;)

    0 讨论(0)
  • 2021-02-02 08:34

    Just copy an example of needed replacement string (new line or else) from code to replacement box.

    0 讨论(0)
  • 2021-02-02 08:35

    You can use [\n\r] to describe newline It's a shame, I came across with that when I tap on magnifier icon next to search bar while finding something in a single file.

    To see several other expressions:

    1. go to a file
    2. do cmd+f and open search
    3. tap on the magnifier icon at the left-hand side of search field at the top
    4. you can see other expressions in the dropdown menu
    0 讨论(0)
  • 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*$

    0 讨论(0)
提交回复
热议问题