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
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
}