将youtube网址转换为iframe嵌入代码

时间:2013-04-05 01:52:00

标签: c# asp.net regex

我一直试图找到一个正则表达式模式,用iframe嵌入代码(C#)替换字符串中的所有youtube网址。显然,必须提取视频ID。以下是应匹配的网址格式:

所有可能的网址都应替换为:

<iframe title='YouTube video player' width='480' height='390' src='http://www.youtube.com/embed/VIDEO_ID_EXTRACTED' frameborder='0' allowfullscreen='1'></iframe>

有人可以指出我正确的方向。

提前谢谢

2 个答案:

答案 0 :(得分:13)

这是正则表达式:

(?:https?:\/\/)?(?:www\.)?(?:(?:(?:youtube.com\/watch\?[^?]*v=|youtu.be\/)([\w\-]+))(?:[^\s?]+)?)

应匹配您发布的所有链接,并将视频ID提取为$1。使用以下代码,您可以使用<iframe/>替换链接:

const string input = "http://www.youtube.com/watch?v=bSiDLCf5u3s " +
                     "https://www.youtube.com/watch?v=bSiDLCf5u3s " +
                     "http://youtu.be/bSiDLCf5u3s " +
                     "www.youtube.com/watch?v=bSiDLCf5u3s " +
                     "youtu.be/bSiDLCf5u3s " +
                     "http://www.youtube.com/watch?feature=player_embedded&v=bSiDLCf5u3s " +
                     "www.youtube.com/watch?feature=player_embedded&v=bSiDLCf5u3s " +
                     "http://www.youtube.com/watch?v=_-QpUDvTdNY";
const string pattern = @"(?:https?:\/\/)?(?:www\.)?(?:(?:(?:youtube.com\/watch\?[^?]*v=|youtu.be\/)([\w\-]+))(?:[^\s?]+)?)";
const string replacement = "<iframe title='YouTube video player' width='480' height='390' src='http://www.youtube.com/embed/$1' frameborder='0' allowfullscreen='1'></iframe>";

var rgx = new Regex(pattern);
var result = rgx.Replace(input, replacement);

// result ==
// <iframe title='YouTube video player' width='480' height='390' src='http://www.youtube.com/embed/bSiDLCf5u3s' frameborder='0' allowfullscreen='1'></iframe>
// ...

答案 1 :(得分:0)

//You can try this:

string strRegex = @"(?<EMBED>(<iframe|<object).*?src=[""'](?<SRC>(http:|https:)?//(www.)?[youtube\.com|youtu.be][^""']+).*?(</iframe>|</object>))";
Regex myRegex = new Regex(strRegex, RegexOptions.Singleline);
string strTargetString = @"<div align=""center""><iframe height=""315"" src=""//www.youtube.com/embed/NiCZAnmjYZ8"" frameborder=""0"" width=""560"" allowfullscreen=""true""></iframe></div> " + "\n" + @"<div align=""center""><iframe height=""315"" src=""//youtube.com/embed/NiCZAnmjYZ81"" frameborder=""0"" width=""570"" allowfullscreen=""""></iframe></div> " + "\n" + @"<div align=""center""><iframe height=""315"" src=""http://www.youtube.com/embed/NiCZAnmjYZ82"" frameborder=""0"" width=""560"" allowfullscreen=""""></iframe></div> " + "\n" + @"<div align=""center""><iframe height=""315"" src=""https://youtube.com/embed/NiCZAnmjYZ83"" frameborder=""0"" width=""560"" allowfullscreen=""""></iframe></div> " + "\n" + @"<div align=""center""><iframe height=""315"" src=""https://youtu.be/embed/NiCZAnmjYZ83"" frameborder=""0"" width=""560"" allowfullscreen=""""></iframe></div> " + "\n" + @"<div align=""center""><iframe height=""315"" src=""http://youtu.be/embed/NiCZAnmjYZ83"" frameborder=""0"" width=""560"" allowfullscreen=""""></iframe></div> " + "\n" + @"<a href=""https://youtu.be/embed/NiCZAnmjYZ83"">Youtube<a>" + "\n" + @"<div style=""text-align:center""><object width=""100%"" height=""100%"" id=""PlayerAS039128cb43804eb7894cba4e8b0220fc"" classid=""clsid:D27CDB6E-AE6D-11cf-96B8-444553540000""><param name=""movie"" value=""http://youtu.be/embed/NiCZAnmjYZ83""></param><param name=""allowFullScreen"" value=""true""></param><param name=""allowscriptaccess"" value=""always""></param><param value=""#000000"" name=""bgcolor""></param><param name=""wmode"" value=""opaque""></param><embed height=""100%"" width=""100%"" quality=""high"" bgcolor=""#000000"" flashvars="""" wmode=""opaque"" allowfullscreen=""true"" allowscriptaccess=""always"" name=""039128cb43804eb7894cba4e8b0220fc"" id=""039128cb43804eb7894cba4e8b0220fc"" style="""" src=""http://youtu.be/embed/NiCZAnmjYZ83"" type=""application/x-shockwave-flash""/></embed></object></div>" + "\n";

foreach (Match myMatch in myRegex.Matches(strTargetString))
{
  if (myMatch.Success)
  {
    // Add your code here
  }
}