Reputation: 1462
I have a Youtube url as an NSString
or Swift String
, 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 web-based programming languages, but none in Objective-C or Swift for Apple platforms...
I'm looking for a method that asks for an NSString url as the parameter and returns the video id as another NSString...
Upvotes: 24
Views: 17687
Reputation: 3838
In Swift 5.1, I created string extension.
func returnYoutubeIDFromURL() -> String? {
let pattern = #"^(?:https?:\/\/)?(?:(?:www\.)?youtube\.com\/(?:(?:v\/)|(?:embed\/|watch(?:\/|\?)){1,2}(?:.*v=)?|.*v=)?|(?:www\.)?youtu\.be\/)([A-Za-z0-9_\-]+)&?.*$"#
let regex = try? NSRegularExpression(pattern: pattern, options: [.caseInsensitive])
let range = NSRange(location: 0, length: self.utf16.count)
if let regexMatch = regex?.firstMatch(in: self, options: [], range: range) {
let videoId = (self as NSString).substring(with: regexMatch.range(at: 1))
print(videoId)
return videoId
}
return nil
}
Usage: "https://youtu.be/YT_ID".returnYoutubeIDFromURL()
will return YT_ID
Upvotes: 0
Reputation: 126157
So a YouTube URL looks something like:
http://www.youtube.com/watch?v=oHg5SJYRHA0
The video ID you're interested in is the part at the end (oHg5SJYRHA0
).... though it's not necessarily at the end, as YouTube URLs can contain other parameters in the query string.
Your best bet is probably to use a regular expression and Foundation's NSRegularExpression
class. I'd presume this approach is used in the other-language tutorials you've found -- note that the content of regular expressions is pretty much the same in any language or toolkit which includes them, so any regex found in those tutorials should work for you. (I'd advise against your approach of breaking on v=
and taking exactly 11 characters, as this is prone to various modes of failure to which a regex is more robust.)
To find the video ID you might want a regex like v=([^&]+)
. The v=
gets us to the right part of the query URL (in case we get something like watch?fmt=22&v=oHg5SJYRHA0
). The parentheses make a capture group so we can extract only the video ID and not the other matched characters we used to find it, and inside the parentheses we look for a sequence of one or more characters which is not an ampersand -- this makes sure we get everything in the v=whatever
field, and no fields after it if you get a URL like watch?v=oHg5SJYRHA0&rel=0
.
Whether you use this or another regex, it's likely that you'll be using capture groups. (If not, rangeOfFirstMatchInString:options:range:
is just about all you need, as seen in Dima's answer.) You can get at the contents of capture groups (as NSTextCheckingResult
objects) using firstMatchInString:options:range:
or similar methods:
NSError *error = NULL;
NSRegularExpression *regex =
[NSRegularExpression regularExpressionWithPattern:@"?.*v=([^&]+)"
options:NSRegularExpressionCaseInsensitive
error:&error];
NSTextCheckingResult *match = [regex firstMatchInString:youtubeURL
options:0
range:NSMakeRange(0, [youtubeURL length])];
if (match) {
NSRange videoIDRange = [match rangeAtIndex:1];
NSString *substringForFirstMatch = [youtubeURL substringWithRange:videoIDRange];
}
Upvotes: 15
Reputation: 3761
Based on this answer: PHP Regex to get youtube video ID?
I adapted a regex for c/objc/c++ string, the important part here is that the regex doesn't get videos from facebook or other services. iOS regex is based on: ICU
NSString *regexString = @"^(?:http(?:s)?://)?(?:www\\.)?(?:m\\.)?(?:youtu\\.be/|youtube\\.com/(?:(?:watch)?\\?(?:.*&)?v(?:i)?=|(?:embed|v|vi|user)/))([^\?&\"'>]+)";
NSError *error;
NSRegularExpression *regex =
[NSRegularExpression regularExpressionWithPattern:regexString
options:NSRegularExpressionCaseInsensitive
error:&error];
NSTextCheckingResult *match = [regex firstMatchInString:message
options:0
range:NSMakeRange(0, [message length])];
if (match && match.numberOfRanges == 2) {
NSRange videoIDRange = [match rangeAtIndex:1];
NSString *videoID = [message substringWithRange:videoIDRange];
return videoID;
}
Matches:
- youtube.com/v/vidid
- youtube.com/vi/vidid
- youtube.com/?v=vidid
- youtube.com/?vi=vidid
- youtube.com/watch?v=vidid
- youtube.com/watch?vi=vidid
- youtu.be/vidid
- youtube.com/embed/vidid
- http://youtube.com/v/vidid
- http://www.youtube.com/v/vidid
- https://www.youtube.com/v/vidid
- youtube.com/watch?v=vidid&wtv=wtv
- http://www.youtube.com/watch?dev=inprogress&v=vidid&feature=related
- https://m.youtube.com/watch?v=vidid
Does not match:
- www.facebook.com?wtv=youtube.com/v/vidid
- https://www.facebook.com/video.php?v=10155279523025107
Upvotes: 3
Reputation: 1532
After spending ages trying to find the correct syntax for the regex, I've come across this which has helped me.
NSString *regexString = @"(?<=v(=|/))([-a-zA-Z0-9_]+)|(?<=youtu.be/)([-a-zA-Z0-9_]+)";
Taken from here. This works for the following URL formats:
- www.youtube.com/v/VIDEOID - www.youtube.com?v=VIDEOID - http://www.youtube.com/watch?v=KFPtWedl7wg&feature=youtu.be - http://www.youtube.com/watch?v=MkTD2Y4LXcM - youtu.be/KFPtWedl7wg_U923 - http://www.youtube.com/watch?feature=player_detailpage&v=biVLGTAMC_U#t=31s
Upvotes: 39
Reputation: 3245
Swift 5
Here is the latest working version I am using. I've added support for YouTube short videos as well. Ex: https://youtube.com/shorts/2xL2WlQM7Nc
extension String{
func extractYoutubeId() -> String? {
let pattern = "((?<=(v|V)/)|(?<=be/)|(?<=(\\?|\\&)v=)|(?<=embed/)|(?<=shorts/))([\\w-]++)"
if let matchRange = self.range(of: pattern, options: .regularExpression) {
return String(self[matchRange])
} else {
return .none
}
}
}
Example:
let ytShortVideoLink = "https://youtube.com/shorts/2xL2WlQM7Nc"
print("Video ID:",ytShortVideoLink. extractYoutubeId())
//Output:
Video ID: 2xL2WlQM7Nc
Upvotes: 2
Reputation: 4785
Update Swift 4:
static func extractYoutubeVideoId(from url: String) -> String? {
let pattern = "((?<=(v|V)/)|(?<=be/)|(?<=(\\?|\\&)v=)|(?<=embed/))([\\w-]++)"
guard let range = url.range(of: pattern, options: .regularExpression) else { return nil }
return String(url[range])
}
Old answer:
A bit swifter way of @Alex's Swift 3 answer w/o the use of NSString:
We can force try the regex, because we know it is valid.
static func extractYoutubeVideoId(from url: String) -> String? {
let pattern = "((?<=(v|V)/)|(?<=be/)|(?<=(\\?|\\&)v=)|(?<=embed/))([\\w-]++)"
let regex = try! NSRegularExpression(pattern: pattern, options: [.caseInsensitive])
let range = NSRange(location: 0, length: url.utf16.count)
guard let firstMatch = regex.firstMatch(in: url, options: .init(rawValue: 0), range: range) else { return nil }
let start = String.UTF16Index(firstMatch.range.location)
let end = String.UTF16Index(firstMatch.range.location + firstMatch.range.length)
return String(url.utf16[start..<end])
}
Or, if you still want NSString:
static func extractYoutubeVideoId(from url: String) -> String? {
let pattern = "((?<=(v|V)/)|(?<=be/)|(?<=(\\?|\\&)v=)|(?<=embed/))([\\w-]++)"
let regex = try! NSRegularExpression(pattern: pattern, options: [.caseInsensitive])
let range = NSRange(location: 0, length: (url as NSString).length)
guard let firstMatch = regex.firstMatch(in: url, options: .init(rawValue: 0), range: range) else { return nil }
return (url as NSString).substring(with: firstMatch.range)
}
Upvotes: 1
Reputation: 1601
Here is RegExp it cover these cases
Objective C
- (NSString *)extractYoutubeIdFromLink:(NSString *)link {
NSString *regexString = @"((?<=(v|V)/)|(?<=be/)|(?<=(\\?|\\&)v=)|(?<=embed/))([\\w-]++)";
NSRegularExpression *regExp = [NSRegularExpression regularExpressionWithPattern:regexString
options:NSRegularExpressionCaseInsensitive
error:nil];
NSArray *array = [regExp matchesInString:link options:0 range:NSMakeRange(0,link.length)];
if (array.count > 0) {
NSTextCheckingResult *result = array.firstObject;
return [link substringWithRange:result.range];
}
return nil;
}
Swift
func extractYoutubeIdFromLink(link: String) -> String? {
let pattern = "((?<=(v|V)/)|(?<=be/)|(?<=(\\?|\\&)v=)|(?<=embed/))([\\w-]++)"
guard let regExp = try? NSRegularExpression(pattern: pattern, options: .CaseInsensitive) else {
return nil
}
let nsLink = link as NSString
let options = NSMatchingOptions(rawValue: 0)
let range = NSRange(location: 0,length: nsLink.length)
let matches = regExp.matchesInString(link as String, options:options, range:range)
if let firstMatch = matches.first {
return nsLink.substringWithRange(firstMatch.range)
}
return nil
}
Swift 3
func extractYoutubeIdFromLink(link: String) -> String? {
let pattern = "((?<=(v|V)/)|(?<=be/)|(?<=(\\?|\\&)v=)|(?<=embed/))([\\w-]++)"
guard let regExp = try? NSRegularExpression(pattern: pattern, options: .caseInsensitive) else {
return nil
}
let nsLink = link as NSString
let options = NSRegularExpression.MatchingOptions(rawValue: 0)
let range = NSRange(location: 0, length: nsLink.length)
let matches = regExp.matches(in: link as String, options:options, range:range)
if let firstMatch = matches.first {
return nsLink.substring(with: firstMatch.range)
}
return nil
}
Upvotes: 71
Reputation: 505
Swift 3 version for @Alex answer
func extractYoutubeIdFromLink(link: String) -> String? {
let pattern = "((?<=(v|V)/)|(?<=be/)|(?<=(\\?|\\&)v=)|(?<=embed/))([\\w-]++)"
guard let regExp = try? NSRegularExpression(pattern: pattern, options: .caseInsensitive) else {
return nil
}
let nsLink = link as NSString
let options = NSRegularExpression.MatchingOptions(rawValue: 0)
let range = NSRange(location: 0,length: nsLink.length)
let matches = regExp.matches(in: link as String, options:options, range:range)
if let firstMatch = matches.first {
debugPrint(firstMatch)
return nsLink.substring(with: firstMatch.range)
}
return nil
}
Upvotes: 0
Reputation: 2689
Swift 2 version for @Alex answer
func getYoutubeVideoId(youtubeLink:String) -> String?{
var youtubeId:String? = nil
let pattern: String = "((?<=(v|V)/)|(?<=be/)|(?<=(\\?|\\&)v=)|(?<=embed/))([\\w-]++)"
do {
let regex = try NSRegularExpression(pattern: pattern, options: .CaseInsensitive)
if let regexMatch = regex.firstMatchInString(youtubeLink, options: NSMatchingOptions(rawValue: 0), range: NSRange(location: 0, length: youtubeLink.characters.count)) {
youtubeId = (youtubeLink as NSString).substringWithRange(regexMatch.range)
}
}
catch let error as NSError{
print("Error while extracting youtube id \(error.debugDescription)")
}
return youtubeId
}
Upvotes: 1
Reputation: 2725
Merging some of your answers, I'd say this is the best answer:
+ (NSString *)extractYoutubeID:(NSString *)youtubeURL
{
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"(?<=v(=|/))([-a-zA-Z0-9_]+)|(?<=youtu.be/)([-a-zA-Z0-9_]+)"
options:NSRegularExpressionCaseInsensitive
error:&error];
NSRange rangeOfFirstMatch = [regex rangeOfFirstMatchInString:youtubeURL options:NSMatchingReportProgress range:NSMakeRange(0, [youtubeURL length])];
if(rangeOfFirstMatch.location != NSNotFound) {
NSString *substringForFirstMatch = [youtubeURL substringWithRange:rangeOfFirstMatch];
return substringForFirstMatch;
}
return nil;
}
Upvotes: 1
Reputation: 842
There is a lot of good answers here but I thought it might be beneficial to some to parse multiple video_ID from a string. This could be a web page or an array of different URL.
Example of page content
NSString *content = @"http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo,http://youtu.be/NLqAF9hrVbY,http://www.youtube.com/watch?v=NLqAF9hrVbY,http://facebook.com,http://www.youtube.com/watch?v=cAcqdjLCN7s";
Method
-(NSArray *)extractVideos:(NSString *)content {
NSString *extractRegex = @"(?<=v(=|/))([-a-zA-Z0-9_]+)|(?<=youtu.be/)([-a-zA-Z0-9_]+)"
NSMutableArray *extractedContent = [[NSMutableArray alloc] init];
if ([content hasPrefix:@"http://"] || [content hasPrefix:@"https://"]) {
NSURL *extractURL = [NSURL URLWithString:content];
if ([extractURL.host rangeOfString:@"youtu"].location != NSNotFound) {
NSRegularExpression *extractRegex = [NSRegularExpression regularExpressionWithPattern:extractRegex options:NSRegularExpressionCaseInsensitive error:nil];
NSArray *extractResults = [extractRegex matchesInString:content options:0 range:NSMakeRange(0, content.length)];
for (NSTextCheckingResult *match in extractResults) {
[extractedContent addObject:[content substringWithRange:match.range]];
}
}
}
return extractedContent;
}
Output
(
NLqAF9hrVbY,
QLqAF9eeVbY,
cAcqdjLCN7s
)
Credit to @Alex for the Regex
Upvotes: 1
Reputation: 416
Here is the swift version using @jt_ik's regex:
func extractYoutubeID(youtubeURL: String) -> String {
var error: NSError?
let pattern: String = "(?<=v(=|/))([-a-zA-Z0-9_]+)|(?<=youtu.be/)([-a-zA-Z0-9_]+)"
let regex = NSRegularExpression(pattern: pattern, options: .CaseInsensitive, error: &error)!
if error == nil {
if let regexMatch = regex.firstMatchInString(youtubeURL, options: nil, range: NSRange(location: 0, length: youtubeURL.utf16Count)) {
return (youtubeURL as NSString).substringWithRange(regexMatch.range)
}
// Handle no match here
return ""
} else {
// Handle error here
println(error?.userInfo)
return ""
}
}
Upvotes: 0
Reputation: 1451
I used the highest voted answer to write a better, more restrictive regex.
NSString *regex = @"(?:youtube.com.+v[=/]|youtu.be/)([-a-zA-Z0-9_]+)";
and then you can get the ID
NSTextCheckingResult *match = [regex firstMatchInString:url
options:0
range:NSMakeRange(0, [url length])];
NSRange videoIDRange = [match rangeAtIndex:1];
NSString *youTubeID = [url substringWithRange:videoIDRange];
Upvotes: 2
Reputation: 21
- (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;
}
Upvotes: 2
Reputation:
You don't even need regexes. The following works regardless of the length of the video ID and its position within the URL:
NSString *vID = nil;
NSString *url = @"http://www.youtube.com/watch?v=cAcqdjLCN7s";
NSString *query = [url componentsSeparatedByString:@"?"][1];
NSArray *pairs = [query componentsSeparatedByString:@"&"];
for (NSString *pair in pairs) {
NSArray *kv = [pair componentsSeparatedByString:@"="];
if ([kv[0] isEqualToString:@"v"]) {
vID = kv[1];
break;
}
}
NSLog(@"%@", vID);
Upvotes: 6
Reputation: 11
Here is my solution which referenced from StackOveFlow. (Youtube I.D parsing for new URL formats)
I did some modification.
///This is the .h
#import <Foundation/Foundation.h>
@interface YoutubeParser : NSObject
+(BOOL) isValidateYoutubeURL:(NSString * )youtubeURL;
+(NSArray *) parseHTML:(NSString *)html ;
@end
///This is the .m
#import "YoutubeParser.h"
@interface YoutubeParser () {
}
@end
@implementation YoutubeParser
#define YOUTUBE_PATTERN @"(https?://)?(www\\.)?(youtu\\.be/|youtube\\.com)?(/|/embed/|/v/|/watch\\?v=|/watch\\?.+&v=)([\\w_-]{11})(&.+)?"
+(NSRegularExpression *)regex {
static NSRegularExpression * regex = nil;
regex = [NSRegularExpression regularExpressionWithPattern:YOUTUBE_PATTERN
options:NSRegularExpressionCaseInsensitive
error:nil];
return regex;
}
+(BOOL) isValidateYoutubeURL:(NSString * )youtubeURL {
NSInteger cnt = [[YoutubeParser regex] numberOfMatchesInString:youtubeURL options:0 range:NSMakeRange(0, [youtubeURL length]) ];
return cnt > 0 ? YES : NO;
}
typedef void (^matching_block_t) (NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop);
+(NSArray *) parseHTML:(NSString *)html {
NSMutableArray * youtubeURLArray = [[NSMutableArray alloc] init];
matching_block_t parseTask = ^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {
NSRange matchRange = [result range];
NSRange youtubeKey = [result rangeAtIndex:5]; //the youtube key
NSString * strKey = [html substringWithRange:youtubeKey] ;
NSLog(@"youtubeKey=%@ , with url=%@ " ,strKey , [html substringWithRange:matchRange]);
[youtubeURLArray addObject:strKey];
};
[[YoutubeParser regex] enumerateMatchesInString:html options:0 range:NSMakeRange(0, [html length]) usingBlock:parseTask ];
return youtubeURLArray;
}
@end
Upvotes: 1
Reputation: 1462
I figured it out by myself...
NSArray *videoURLSplit = [videoURL componentsSeparatedByString:@"v="];
NSString *videoID = [[videoURLSplit objectAtIndex:1] substringToIndex:11];
NSLog(@"%@",videoID);
Very simple... All video urls contain v=VIDEO_ID I just separated the url into an array and then took the first 11 digits of what is after v= since there could be more http GET information in the url...
Thanks for helping though!
Upvotes: -2
Reputation: 23634
The tutorials you are probably seeing are just instructions on how to use regular expressions, which is also what you want to use in this case.
The Cocoa class you will need to use is NSRegularExpression.
Your actual regex string will depend on the format you are expecting the url to be in since it looks like youtube has several. The general function will look something like:
+ (NSString *)extractYoutubeID:(NSString *)youtubeURL
{
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"your regex string goes here" options:NSRegularExpressionCaseInsensitive error:&error];
NSRange rangeOfFirstMatch = [regex rangeOfFirstMatchInString:youtubeURL options:0 range:NSMakeRange(0, [youtubeURL length])];
if(!NSEqualRanges(rangeOfFirstMatch, NSMakeRange(NSNotFound, 0)))
{
NSString *substringForFirstMatch = [youtubeURL substringWithRange:rangeOfFirstMatch];
return substringForFirstMatch;
}
return nil;
}
Upvotes: 9