我在旧网站上工作,我需要修复我的YouTube链接。 例如,我有以下内容的字符串变量:“suEGD8aaSzI列表& playauto = 1”C#正则表达式 - 提取YouTube查询字符串参数
<h1>title</h1>
<p>
some text here
.......
<iframe src="http://www.youtube.com/embed/suEGD8aaSzI?list&playauto=1" width="560" height="315" frameborder="0" scrolling="auto"></iframe>
.......
</p>
</p>
我试图让参数之外, 做到以下几点:
lblContent.Text = Regex.Replace(ArticleContent, @"myRegularExpressionHere", "https://www.youtube.com/embed/$1", RegexOptions.IgnoreCase);
到目前为止,它的最好的是我能找到的:
https?:\/\/(?:[0-9A-Z-]+\.)?(?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|watch\?v\=))([\w-]{10,12})(?:[\&\?\#].*?)*?(?:[\&\?\#]t=([\dhm]+s))?(?=")
,但它是不够的,因为我只得到“suEGD8aaSzI”一一个参数,查询字符串的其余部分:“?list & playauto = 1”不包括在里面。
帮助将非常感激。
尝试拆分(/)上的url字符串,并且最后一个索引将为您提供complate所需的字符串。但只有在你的查询字符串中没有任何斜线时,这才会起作用。 – Manish
基本上,查询从**?**符号开始,因此您可以执行** meUrl = meUrl.Substring(meUrl.LastIndexOf(“?”))**以仅获取查询字符串。然后你可以通过** var query = meUrl.Split(“&”,SplitOptions.IgnoreEmptyEntities)**解析查询字符串。现在变量**查询**应该包含类似** {“q1 = 123”,“q2 = somehting”} **的内容。 –
问题是我在这个字符串变量里面有一个完整的html内容,所以我仍然需要提取这些外部的youtube链接。 – serg90