Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
329 views
in Technique[技术] by (71.8m points)

iphone - Find/Replace in Xcode using Regular Expression

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

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

In those lines, I want to search for the following,

[myClass doOperationOne
[myClass doOperationTwo
[myClass doOperationThree

And replace them with the following, (by appending WithOptions:[Permissions isOptionsAvailable])

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

How can I do this using single Regular Expression Find/Replace?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

NOTE: The behavior changed in Xcode 6. The 123 syntax was replaced with $123. Also be warned that newlines can now be matched in reg exps so be sure to skim before Replace-Alling

Adding an additional argument to a method:

To replace

[* setFoo:*]

with

[* setFoo:* bar:value]

you do

(setFoo:)(.*)(])
$1$2 bar:value]

(search string and replacement string respectively).

or, if on Xcode 5 or older

(setFoo:)(.*)(])
12 bar:value]

(below uses new syntax; swap if on Xcode 5 or older)

NOTE: Everything written here applies to the string-replacement methods in NSString/NSMutableString when using NSRegularExpressionSearch!

In other words, this line:

[input replaceOccurrencesOfString:@"
\[([^\]^
]*)\]
" 
                       withString:@"
///

[$1]
" 
                          options:NSRegularExpressionSearch
                            range:(NSRange){0,input.length}];

will convert all "[...] " sequences (leaving e.g. "[... ..." alone!) into " /// [...] ", preserving ... using $1.

Always did this by hand but in this case, I was adding an OPTIONAL 'animate:' flag, and the default up to this point had been YES, but I wanted NO, so every call had to be updated.

More examples:

Deprecated methods (iOS)

dismissModalViewControllerAnimated:... deprecation

To fix the deprecated dismissModalViewController replacing it with an empty completion block and retaining animated value:

(dismissModalViewControllerAnimated:)(.*)(])
dismissViewControllerAnimated:$2 completion:nil]

presentModalViewController:animated: deprecation

(presentModalViewController:)(.*)( animated:)(.*)(])
presentViewController:$2$3$4 completion:nil]

Miscellaneous

PD...Release → PD...Destroy

I recently wrote a c library with a bunch of files with the prefix PD and I used Create/Release as malloc/free keywords, which I regretted as it may make people think retain counting is kept, so I wanted to renamePD<anything>Release( with PD<anything>Destroy(.

([

 ])(PD)(.*)Release(
$1$2$3Destroy(

Since Core Graphics has CGPDFDocumentRelease and similar, I had to ensure the word started with PD as well.


PDAssert(PDScannerPop...(...));

I had stupidly put assertions around functional code that would become empty when !#ifdef DEBUG. Luckily I knew that all of these started with PDAssert(PDScannerPop.... and ended with );.

(PDAssert()(PDScannerPop)(.*)();)
$2$3;

No $1 here because that would include the PDAssert( again. Note that I've split right after the PDAssert( and am leaving out the ) in ); in the 3rd chunk which removes the surplus parens from removing PDAssert(.

Dealing with end parentheses

You can match everything except ")" to deal with over-greedy regexp replaces. Example:

foo(replace(arg), bar)
foo(newvalue(newarg), bar)

Using replace((.*)) will grab replace(arg), bar) and result will be foo(newvalue(newarg)! Instead use replace(([^)]*)) which will grab replace(arg) and leave , bar) alone.

Converting a bunch of NSString properties from using retain and/or strong (and whatever else) to using copy

@property (([^)]*)[sr][te][rt][oa][ni][gn]([^)]*))(.*)NSString(.*)
@property ($1copy$2)$3NSString$4

The weird sr te rt thing in the center matches both "strong" and "retain".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...