Sunday, August 28, 2011

Regular Expressions for Those Nasty Autorelease Statements in ARC Migration

Maybe I'm missing something and the "migrate to ARC" tool in the XCode 4.2 Beta replaces your autorelease statements, but that's not what I've found. Please correct me if I'm wrong.

Here's a simple regex replace in Textmate for changing lines like:

[[[Thing alloc] initWithWhatever:whoever] autorelease];

to 

[[Thing alloc] initWithWhatever:whoever];

The regex is 

(\[\[\[)([^\]]*\][^\]]*\])(\ autorelease\]);

and the replacement string is:

[[$2;

Then I needed to fix these lines

for (Thinger *thing in [thingers copy] autorelease])

and I used regex:

(\[)([^\]]*\][^\]]*)(\ autorelease\])

and the replacement string is:

$2


5 comments:

Alex Gray said...

got any more brilliance left in you for retains? And Maybe YOU can tell me why of all things that each and every implementation of regular expressions, are anything but regular? Oh the irony of it all.

Alex Gray said...

got any more brilliance left in you for retains? And Maybe YOU can tell me why of all things that each and every implementation of regular expressions, are anything but regular? Oh the irony of it all.

Dan Rosenstark said...

Um, what? Regular expression implementations are all more similar than different, I think. Are you finding big differences?

Ziggy said...

Use this one instead:

(\[)(.*){0,100}(\ autorelease\])

and replace with $1$2

then the same, but change "autorelease" for "retain" and "release".
and it will solve all your problems.

alex peterson said...

Might I suggest modifying the first regex to include additional open brackets.

Like so:
(\[\[\[)([^\[]*[^\]]*\][^\]]*\])(\ autorelease\]);