正規表現を使う

iPhoneで正規表現を使うにはRegexKitLiteを使用します。

ダウンロードしたファイルを展開し、RegexKitLite.hRegexKitLite.m二つのファイルをプロジェクトに追加します。 文字列のインスタンスに対してさまざまなメソッドを実行できます。

使い方はドキュメントに様々なサンプルが載っています。

RegexKitLite.hをインポートします。

#import "RegexKitLite.h"
searchString = @"http://www.example.com:8080/index.html"; 
regexString = @"\\bhttps?://[a-zA-Z0-9\\-.]+(?::(\\d+))?(?:(?:/[a-zA-Z0-9\\-._?,'+\\&%$=~*!():@\\\\]*)+)?"; 
NSInteger portInteger = [[searchString stringByMatching:regexString capture:1] integerValue];
 
NSLog(@"portInteger: '%ld'", portInteger); 
// 2008-10-15 08:52:52.500 host_port[8021:807] portInteger: '8080'

外部リンク


Personal Tools