objective-cの正規表現はめんどくさい。
そこでRegexKitLite。これをimportするとNSStringクラスに正規表現な諸々の関数が追加される。
stringByMatchingを呼ぶだけで一致文字列を取得
#import "RegexKitLite.h"
...
NSString *hoge = @"Hello world!!!";
NSString *match = [hoge stringByMatching:@"[a-z]*!"];
NSLog (@"%@", match); // world!
マッチグループを配列に入れるのも簡単
NSString *url = @"http://hogehoge/api/city=1234";
NSArray *matchArray = [url captureComponentsMatchedByRegex:@"city=([0-9]*)"];
NSLog(@"%@", matchArray[0]); // city=1234
NSLog(@"%@", matchArray[1]); // 1234
もちろんCocoaPodsにもあるよ
pod 'RegexKitLite', '4.0'