Skip to content

Instantly share code, notes, and snippets.

@davecowart
Created August 27, 2012 22:11
Show Gist options
  • Save davecowart/3492791 to your computer and use it in GitHub Desktop.
Save davecowart/3492791 to your computer and use it in GitHub Desktop.
Obj-c YouTube Regex
NSError *error = nil;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(?<=(?:v|i)=)[a-zA-Z0-9-]+(?=&)|(?<=(?:v|i)\\/)[^&\n]+|(?<=embed\\/)[^\"&\n]+|(?<=‌​(?:v|i)=)[^&\n]+|(?<=youtu.be\\/)[^&\n]+" options:NSRegularExpressionCaseInsensitive error:&error];
NSString *link = [post objectForKey:@"link"];
NSTextCheckingResult *match = [regex firstMatchInString:link options:0 range:NSMakeRange(0, [link length])];
NSString *identifier = [link substringWithRange:match.range];
NSString *html = [self embedYouTube:identifier frame:webView.frame];
[webView loadHTMLString:html baseURL:nil];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment