Find/Replace in Xcode using Regular Expression
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:)(.*)(\])
\1\2 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:@"\n\\[([^\\]^\n]*)\\]\n"
withString:@"\n///\n\n[$1]\n"
options:NSRegularExpressionSearch
range:(NSRange){0,input.length}];
will convert all "[...]\n"
sequences (leaving e.g. "[...\n..."
alone!) into "\n///\n\n[...]\n"
, 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(
.
([\n\r ])(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".
Somehow I've managed to find the answer (which is enough for my need here) by referring the post: http://www.cocoabuilder.com/archive/xcode/273123-how-to-use-regular-expressions-in-xcode-find-replace.html, and trial and error method.
My Find string is:
(\[myClass.myMethod )(.*)(\];)
And, my Replace string is:
\1\2WithOptions:[Permissions isOptionsAvailable]\3
Please post if there is any better way than this..