Reputation: 115
I've been trying to find a regex pattern to replace all youtube URLs in a string with the iframe embed code (C#). Obviously the video ID has to extracted. Here is the url patterns that should match:
all possible urls should be replaced with:
<iframe title='YouTube video player' width='480' height='390' src='http://www.youtube.com/embed/VIDEO_ID_EXTRACTED' frameborder='0' allowfullscreen='1'></iframe>
Can someone please point me to a right direction.
Thank you in advance
Upvotes: 6
Views: 8757
Reputation: 11058
Here is the regex:
(?:https?:\/\/)?(?:www\.)?(?:(?:(?:youtube.com\/watch\?[^?]*v=|youtu.be\/)([\w\-]+))(?:[^\s?]+)?)
Should match all the links you posted and extracts the video ID as $1
. And with the following code you replace the links with the <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='https://www.youtube.com/embed/bSiDLCf5u3s' frameborder='0' allowfullscreen='1'></iframe>
// ...
Upvotes: 18
Reputation: 1
//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
}
}
Upvotes: 0