c#正则表达式过滤youtube视频的ID [英] c# Regex to filter the ID of a youtube video
问题描述
我知道有类似的问题,但实际上我还没有看到一个涵盖所有可能的 youtube 网址的问题.我已经涵盖了除 2 种可能性之外的几乎所有内容的正则表达式.
(?<=(https?\:\/\/)?(m\.)?(www\.)?(youtube\.com|youtube\.de|youtu\.?be|youtube-nocookie\.com)/(.*?)(/|embed|vi?=|\?vi?=|watch\?|watch\?vi?\=?|watch\?v%|a=|u/1))([A-Za-z0-9_-]{11})
我过滤 URL 的开头,直到 .com 之后.然后,直到 11 个字符 ID 之前链接中的内容都无关紧要.由于除了 ID 之外,[A-Za-z0-9_-]{11} 还有其他可能的匹配项,我已经包含了 ID 的所有可能的前缀 (/|embed|vi...).现在正则表达式不起作用的 2 个可能情况是
https://www.youtube.com/user/IngridMichaelsonVEVO#p/a/u/1/QdK8U-VIH_o"
和
第一个是因为在/之后有一个 11 个字符的字符串(Ingrid...)第二个是因为每个其他 URL 在 ID 之前都有多个/.我不知道如何解决这个问题.对于我尝试过的用户 URL (?!.(user/(.?)/)) 排除用户/随便/之后的所有内容,但这只会删除整行的匹配项,即使ID 和用户名都匹配.我知道该链接实际上没有 ID,因为它链接到一个配置文件,问题是它向我展示了除了我的正则表达式过滤的 ID 之外还有另一个字符串的可能性
对于第二个,我不知道如何解决它,因为在我看来,每个其他 URL 中都需要 2/.
这是我正在使用的列表
https://gist.github.com/rodrigoborgesdeoliveira/987683cfbcf>987683cfbcfb73
使用额外的 iframe 进行测试
非常感谢
请试试下面的正则表达式.捕获的组 1 将具有视频 ID.
(?:(?:(?<=[vi])(?:=|%3D)|\/))([A-Za-z0-9_-]{11})[&?\s#%\"]
I am aware that there are similar questions but I haven't actually seen one that goes over every possible youtube url. The Regex that I have already covers almost everything but 2 eventualities.
(?<=(https?\:\/\/)?(m\.)?(www\.)?(youtube\.com|youtube\.de|youtu\.?be|youtube-nocookie\.com)/(.*?)(/|embed|vi?=|\?vi?=|watch\?|watch\?vi?\=?|watch\?v%|a=|u/1))([A-Za-z0-9_-]{11})
I filter for the beginning of the URL until after the .com. Then it doesn't matter what is in the link until the 11 char ID. Since there are other possible matches for [A-Za-z0-9_-]{11} except the ID, I have included every possible prefix for the ID (/|embed|vi...). Now the 2 eventualities where the Regex doesn't work are
https://www.youtube.com/user/IngridMichaelsonVEVO#p/a/u/1/QdK8U-VIH_o"
and
the first because there is a 11 char string right after a / (Ingrid...) and the second one because every other URL has more than one / before the ID. I'm not sure how to solve this. For the user URL I have tried (?!.(user/(.?)/)) to exclude everything after a user/whatever/ but that just deletes the match of the whole line, even if the ID was matched as well as the username. I am aware that the link doesn't really have an ID since it links to a profile, the problem is that it has shown me the possibility of there being another string besides the ID that my regex filters
For the second one I have no idea how to solve it because in my eyes the 2 / are needed in every other URL.
Here's the list I'm working with
https://gist.github.com/rodrigoborgesdeoliveira/987683cfbfcc8d800192da1e73adc486
With an additional iframe for testing
<iframe width="560" height="315" src="https://www.youtube.com/embed/BKapPQxcR3Q" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
Thanks a lot
Please try below regex. Captured group 1 will have video id.
(?:(?:(?<=[vi])(?:=|%3D)|\/))([A-Za-z0-9_-]{11})[&?\s#%\"]
这篇关于c#正则表达式过滤youtube视频的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!