从链接获取 youtube id
我得到了这个代码来从链接中获取youtube id,例如 www.youtube.com/watch?v=xxxxxxx
URL youtubeURL = new URL(link);
youtubeURL.getQuery();
基本上这会很容易地让我得到 id v=xxxxxxxx
但我注意到有时 youtube 链接会像这样
http://gdata.youtube.com/feeds/api/videos/xxxxxx
我从提要中获取链接 那么我是否需要为此构建一个正则表达式,或者有一个解析器来为我获取它?
I got this code to get the youtube id from the links like
www.youtube.com/watch?v=xxxxxxx
URL youtubeURL = new URL(link);
youtubeURL.getQuery();
basically this will get me the id easily v=xxxxxxxx
but I noticed sometime youtube links will be like this
http://gdata.youtube.com/feeds/api/videos/xxxxxx
I am getting the links from a feed
so do I need to build a regex for that or theres a parser to get that for me ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
尝试了其他方法,但在我的情况下失败了 - 调整正则表达式以适合我的网址
这一个适用于:(您还可以实施安全检查 youtubeid length = 11 )
Tried the other ones but failed in my case - adjusted the regex to fit for my urls
This one works for: (you could also implement a security check youtubeid length = 11 )
这个正则表达式可以解决这个问题:
这意味着我们首先寻找
video/
或v=
然后捕获单词中可以出现的所有以下字符(字母、数字) 、和下划线)和连字符。java 中的示例:
输出:
This regex would do the trick:
This means that we're first looking for
video/
orv=
then captures all the following characters that can be in word (letters, digits, and underscores) and hyphens.Example in java:
Output:
从此链接获得了更好的解决方案。
使用以下方法从链接中获取videoId。
希望这有帮助。
Got a better solution from this link.
Use the following method to get the videoId from the link.
Hope this helps.
这种模式对我有用:
来源:youtube 链接的正则表达式
That pattern worked for me:
source: Regular expression for youtube links
在不知道所有可能的 YouTube URL 的完整规范的情况下,这似乎适用于您提供的示例:
...与这些 URL 中的任何一个匹配
PjDw3azfZWI
:您需要更多信息才能获得特定的信息(如果您不知道这些来自 YouTube),尽管这是一个非常快速的检查
请记住,如果您尝试仅使用
getQuery()
方法的结果,则这是不可能的从中提取结果http://gdata.youtube.com/feeds/api/videos/PjDw3azfZWI
URL,因为此 URL 没有查询部分...Java 示例:
Without knowing the complete specification for all the possible YouTube URLs, this seems to work for the examples you provided:
... matches
PjDw3azfZWI
from either of these URLS:You would need a little more to get that particular info if you did not know that these were from youtube, though that's a pretty quick check
Keep in mind that if you are trying to use only the result of the
getQuery()
method, it will not be possible to extract the result from thehttp://gdata.youtube.com/feeds/api/videos/PjDw3azfZWI
URL, as this URL does not have a query part to it...Java Example:
这对我有用
来源链接
This was worked for me
Source link
这不使用正则表达式,但仍然可以完成这项工作。
This doesn't use a regex but should still do the job.
这是
kotlin
版本,它将支持许多 Youtube 网址,包括Shorts:以下是我测试过的一些示例:
尝试一下regex101.com 在左侧菜单中选择 Java
Here is the
kotlin
version that will support many of Youtube urls including Shorts:Here are some samples to I've tested against:
Try it on regex101.com choosing Java on the left menu