NSRegularExpressionを用いる。
使い方
クラスメソッド
NSRegularExpression regularExpressionWithPattern:options:error:
- パターンに正規表現の文字列@"abc.*ccc"等、
で正規表現を作り、
[regExp firstMatchInString:string options range:NSMakeRange(0,string.length)];
で結果、NSTextCheckingResult を得る。
- match.numberOfRangesでマッチした数
- [match rangeAtIndex:a]でa番目にマッチした文字列の範囲
- [string substringWithRange:[match rangeAtIndex:a]]でa番目のマッチした文字列を取り出せる。
正規表現の参考
NSMatchingOptions
typedef NS_OPTIONS(NSUInteger, NSMatchingOptions) {
NSMatchingReportProgress = 1 << 0, /* Call the block periodically during long-running match operations. */
任意のマッチング操作のlong-runningの間中周期的にブロックを呼ぶ
NSMatchingReportCompletion = 1 << 1, /* Call the block once after the completion of any matching. */
任意のマッチングの後にブロックを一度だけ呼び出す
NSMatchingAnchored = 1 << 2, /* Limit matches to those at the start of the search range. */
NSMatchingWithTransparentBounds = 1 << 3, /* Allow matching to look beyond the bounds of the search range. */
探索範囲の制限を超えて見るようなマッチングを許す
NSMatchingWithoutAnchoringBounds = 1 << 4 /* Prevent ^ and $ from automatically matching the beginning and end of the search range. */
ハットやドル記号が探索範囲の開始と終わりにに自動的にマッチングされるのを防ぐ
};
最終更新:2013年01月23日 14:20