Created
September 20, 2012 22:52
-
-
Save tatsuro-ueda/3758826 to your computer and use it in GitHub Desktop.
Objective-Cで正規表現でエスケープするには\\(ダブルバックスラッシュ)を使う
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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