Youtube视频ID来自URL - Objective C [英] Youtube Video ID From URL - Objective C

查看:154
本文介绍了Youtube视频ID来自URL - Objective C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上有一个Youtube url作为NSString,但我需要提取显示在网址的视频ID。我发现很多教程如何在PHP或其他基于Web的编程语言,但没有一个在客观的c ...

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 web-based programming languages, but none in objective c...

我基本上寻找一个方法,它要求一个NSString url作为参数,并返回视频id作为另一个nsstring ...

I'm basically looking for a method that asks for an NSString url as the parameter and returns the video id as another nsstring...

任何人都知道如何做或任何教程在那里? p>

Anyone know how to do that or any tutorials out there?

推荐答案

因此,YouTube网址如下所示:

So a YouTube URL looks something like:

http://www.youtube.com/watch?v=oHg5SJYRHA0

您感兴趣的视频ID是结尾处的部分( oHg5SJYRHA0 )....虽然它不一定是结束,YouTube网址可以包含查询字符串中的其他参数。

The video ID you're interested in is 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.

您最好的选择是使用正则表达式和Foundation的 NSRegularExpression 类。我认为这种方法是用在你发现的其他语言教程中 - 注意正则表达式的内容在任何语言或工具包中包含它们是相同的,所以在这些教程中找到的任何正则表达式应该工作为你。 (我建议你反对你的方法打破 v = ,并采取11个字符,因为这是易于各种模式的故障,正规表达式更强大。

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.)

要查找视频ID,您可能需要一个正则表达式,例如 v =([^&] +) v = 让我们到查询网址的正确部分(如果我们得到像 watch?fmt = 22& v = oHg5SJYRHA0 )。括号是一个捕获组 ,所以我们可以只提取视频ID,而不是我们用来找到它的其他匹配的字符,在括号内,我们寻找一个或多个字符的序列,不是一个和号 - 这确保我们得到 v = whatever 字段中的一切,后面没有字段,如果你得到一个像 watch?v = oHg5SJYRHA0& rel = 0

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.

无论您使用这个还是另一个正则表达式,很可能会使用捕获组。 (如果不是,请 rangeOfFirstMatchInString:options:range: 就是你所需要的,就像Dima的答案所示)。你可以得到捕获组(作为 NSTextCheckingResult 对象)使用 firstMatchInString:options:range: 或类似方法: p>

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

这篇关于Youtube视频ID来自URL - Objective C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆