@implementation ViewController
– (void)viewDidLoad
{
[superviewDidLoad];
NSString *frogSong = @”かえるぴょこぴょこみぴょこぴょこあわせてぴょこぴょこみぴょこぴょこ“;
NSRegularExpression *reg = [[NSRegularExpressionalloc] initWithPattern:@”(.*)ぴょこぴょこ(.*)ぴょこぴょこ(.*)ぴょこぴょこ(.*)ぴょこぴょこ“ options:0 error:nil];
NSArray *matches = [reg matchesInString:frogSong options:0 range:NSMakeRange(0, [frogSong length])];
for (NSTextCheckingResult *result in matches) {
NSLog(@”結果リスト数: %d”, [result numberOfRanges]);
for (int i = 0; i < [result numberOfRanges]; i++) {
NSRange r = [result rangeAtIndex:i];
// 0番目は全体、以降、パターンで指定した括弧に該当する文字列
NSLog(@”検索結果 %@”, [frogSong substringWithRange:r]);
}
}
}
結果リスト数: 5
NSTextCheckingResultの中身 かえるぴょこぴょこみぴょこぴょこあわせてぴょこぴょこみぴょこぴょこ
NSTextCheckingResultの中身 かえる
NSTextCheckingResultの中身 み
NSTextCheckingResultの中身 あわせて
NSTextCheckingResultの中身 み