Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tatsuro-ueda/3758826 to your computer and use it in GitHub Desktop.
Save tatsuro-ueda/3758826 to your computer and use it in GitHub Desktop.
Objective-Cで正規表現でエスケープするには\\(ダブルバックスラッシュ)を使う
NSError *error = nil;
NSRegularExpression *regexp = [NSRegularExpression regularExpressionWithPattern:@"\"http://.+\\.smilevideo\\.jp/smile\\?i=[0-9]{8}+\"" options:0 error:&error];
// エラーならば表示する
if (error != nil) {
NSLog(@"%@", error);
}
// find by regular expression
NSTextCheckingResult *match =
[regexp firstMatchInString:str options:0 range:NSMakeRange(0, str.length)];
// get the first result
NSRange resultRange = [match rangeAtIndex:0];
if (match) {
// get the og:image URL from the find result
NSRange urlRange = NSMakeRange(resultRange.location + 1, resultRange.length - 2);
// og:image URL
return [NSURL URLWithString:[str substringWithRange:urlRange]];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment