<meta property="og:image" content="http://kokoga.hosii/address/desu.jpg" />
検索や置換、正規表現などを頑張ってみたのですが、どうにもうまく行きません。
どうぞよろしくお願いします。
コメントでソースが欠けてしまうので再回答させて頂きます。
NSString *string = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://q.hatena.ne.jp/1342645668"] encoding:NSUTF8StringEncoding error:nil]; NSError *error = nil; NSRegularExpression *regexp = [NSRegularExpression regularExpressionWithPattern:@"<meta property=\"og:image\" content=\".+\"" options:0 error:&error]; if (error != nil) { NSLog(@"%@", error); } else { NSTextCheckingResult *match = [regexp firstMatchInString:string options:0 range:NSMakeRange(0, string.length)]; NSRange resultRange = [match rangeAtIndex:0]; NSLog(@"match=%@", [string substringWithRange:resultRange]); NSRange urlRange = NSMakeRange(resultRange.location + 35, resultRange.length - 35 - 1); NSLog(@"url =%@", [string substringWithRange:urlRange]); }
ほかにもっと簡潔なやりかたがあるかもしれませんが、
こんな感じで取得できました。
正規表現で抜いたあと、「content="」と「"」の間を取り出しています
NSString *string = @"<meta property=\"og:image\" content=\"http://kokoga.hosii/address/desu.jpg\" />"; NSError *error = nil; NSRegularExpression *regexp = [NSRegularExpression regularExpressionWithPattern:@"content=\".+\"" options:0 error:&error]; if (error != nil) { NSLog(@"%@", error); } else { NSTextCheckingResult *match = [regexp firstMatchInString:string options:0 range:NSMakeRange(0, string.length)]; NSRange resultRange = [match rangeAtIndex:0]; NSRange urlRange = NSMakeRange(resultRange.location + 9, resultRange.length - 9 - 1); NSLog(@"%@", [string substringWithRange:urlRange]); }