Using regular expressions in iPhone/Cocoa Touch apps

I thought I'd blogged about this earlier but apparently not: if you want to easily use regular expressions (regex) in your iPhone apps, take a look at the RegexKitLite project.

Adding it to your app simply involves two steps:

  1. Drag RegexKitLite.m and RegexKitLite.h to your Xcode project
  2. Under Project → Edit Project Settings → Other Linker Flags, add -licucore

That's it!

The regular expression methods are set up as a category on NSString, so you can, for example:

NSArray *results = [@"Some string 123" captureComponentsMatchedByRegex:@"\d"];
NSString *coolString =  [@"That's hot!" stringByReplacingOccurrencesOfRegex:@"h.t" withString:@"cool"];