Get Youtube Video ID
+ (NSString*) getVideoID:(NSString*)linkString
{
NSMutableString *predicateString = [[NSMutableString alloc] initWithFormat:@"%@", linkString];
NSString *regexStr = @"[a-zA-Z0-9_-]{10}";
NSError *error;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexStr
options:NSRegularExpressionCaseInsensitive
error:&error];
__block NSString *retunString = @"";
[regex enumerateMatchesInString:predicateString options:0 range:NSMakeRange(0, [predicateString length]) usingBlock:^(NSTextCheckingResult *match, NSMatchingFlags flags, BOOL *stop){
NSRange matchRange = [match range];
matchRange.length = matchRange.length+1;
//NSLog(@"matchRange.location: %d",matchRange.location); //31
//NSLog(@"matchRange.length: %d",matchRange.length); //11
//NSLog(@"result: %@", [predicateString substringWithRange:matchRange]); //tTfj05VNT40
retunString = [predicateString substringWithRange:matchRange];
}];
return retunString;
}
Labels:
iphone SDK,
regex