Regex – iPhone/Cocoa: NSPredicate regex replace

cocoaiphoneregex

According to this guide, I can use NSPredicate to do regex matching on strings, i.e., the perl equivalent of $my_string =~ m/[some regex]/

But can I do regex replace, i.e. the equivalent of this perl expression: $my_string =~ s/[pattern]/[replacement]/g ?

Best Solution

Google Toolbox for Mac has some useful regex functionality. It works on the iPhone. Take a look at GTMRegex.h. There is a category on NSString with this method:

- (NSString *)gtm_stringByReplacingMatchesOfPattern:(NSString *)pattern withReplacement:(NSString *)replacementPattern;