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...
The video ID will be located in the URL of the video page, right after the v= URL parameter. In this case, the URL of the video is: https://www.youtube.com/watch?v=aqz-KE-bpKQ. Therefore, the ID of the video is aqz-KE-bpKQ .
The logic is change the last letter with the consecutive letter, the letters are case sensitive. So if the last letter is 'a' change it to 'b', if 'A' change it to 'B', if '1' change it to '2'.
As Scott explains, YouTube's video ID system, which can be seen in any URL that leads to one of the site's uploads, uses a 64-character language. That means, for each of the 11 spaces in one Video ID, there are 64 different characters that can be used.
A YouTube Video ID is a unique ID to identify a video which is uploaded to YouTube. A YouTube video ID is used to create a unique URL to show the video and can be used to embed a YouTube video on any website. It's not possible to change a video ID of a video.
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 }
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With