Find/Replace in Xcode using Regular Expression

前端 未结 6 1613
不思量自难忘°
不思量自难忘° 2020-12-04 10:43

I have the following function calls at several places in my class.

[myClass doOperationOne];
[myClass doOperationTwo];
[myClass doOperationThree];

6条回答
  •  忘掉有多难
    2020-12-04 11:33

    Note for Xcode 8

    I was trying to update my answer below for Xcode 8, but I couldn't do it. If you have the answer, please let me know.

    Xcode 7

    Unless you were already a regular expression genius, this much easier to do in Xcode now. You don't need to use regular expressions at all (but you can if you still want to).

    Example

    Replace these strings:

    [myClass doOperationOne];
    [myClass doOperationTwo];
    [myClass doOperationThree];

    with these strings

    [myClass doOperationOneWithOptions:[Permissions isOptionsAvailable]];
    [myClass doOperationTwoWithOptions:[Permissions isOptionsAvailable]];
    [myClass doOperationThreeWithOptions:[Permissions isOptionsAvailable]];

    The bolded numbers show why a plain Find and Replace won't work.

    Setup

    There are two ways you can do Find and Replace: Textual and Regular Expression

    Click the magnifying glass

    and "Edit Find Options..."

    and choose one of them.

    Method 1 - Textual

    • Enter [myClass doOperation in Find

    • Click the magnifying class and choose "Insert Pattern"

    • Choose "Any Word Characters"

    • Add ]; to Find

    • Repeat for the Replacement String but use WithOptions:[Permissions isOptionsAvailable]]; for the last part.

    • A single Replace All will work now.

    Method 2 - Regular Expression

    • Make sure that Regular Expressions are selected as described in Setup above.

    • Use (\[myClass doOperation)(.*)(\];) for the Regular Expression

    • Use $1$2WithOptions:[Permissions isOptionsAvailable]$3 for the Replacement String. The $1, $2, and $3 replace the parentheses in the regular expression.

    • A single Replace All will work now.

    • See this link for more help with regular expressions.

    See also

    • How can I find and replace inside a selection in Xcode?

提交回复
热议问题