5

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

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

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

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

先感谢您

4

2 回答 2

17

这是正则表达式:

(?: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>
// ...
于 2013-04-05T02:03:09.000 回答
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
  }
}
于 2014-12-20T03:31:21.550 回答