I basically have a Youtube url as an NSString, but I need to extract the video id that is displayed in the url. I found many tutorials on how to do this in php or and other
- (NSString*)getYoutubeVideoID:(NSString*)url {
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(?<=watch\\?v=|/videos/|embed\\/)[^#\\&\\?]*"
options:NSRegularExpressionCaseInsensitive
error:&error];
NSTextCheckingResult *match = [regex firstMatchInString:url
options:0
range:NSMakeRange(0, [url length])];
NSString *substringForFirstMatch;
if (match) {
NSRange videoIDRange = [match rangeAtIndex:0];
substringForFirstMatch = [url substringWithRange:videoIDRange];
}
return substringForFirstMatch;
}