アプリなどを開発するブログ

React Native / Swift / Ruby on Railsなどの学習メモ。


正規表現でHTML内のimg srcのURLを抜き出す(Objective-C)

NSString *html = @"HTMLの内容";
NSRegularExpression *regexp01 = [[NSRegularExpression alloc] initWithPattern:@"(<img.*?src=\")(.*?)(\".*?>)"
   options:0
         error:nil];
           
NSArray *results = [regexp01 matchesInString:html options:0 range:NSMakeRange(0, html.length)];

for( int i = 0; i < results.count; i++) {
    NSTextCheckingResult *result = [results objectAtIndex:i];
    NSString *imgURL = [html substringWithRange:[result rangeAtIndex:2]];
    NSLog(@"imgURL : %@", imgURL);
}