LoginSignup
21
20

More than 5 years have passed since last update.

Obj-Cで正規表現使うならRegexKitLite

Posted at

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'
21
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
21
20