5

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

ShyGuy82
  • 105
  • 2
  • 6

2 Answers2

16

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='http://www.youtube.com/embed/bSiDLCf5u3s' frameborder='0' allowfullscreen='1'></iframe>
// ...
Linus Caldwell
  • 10,316
  • 12
  • 43
  • 56
  • Hello, thank you for your help but the replacement sets the wrong SRC. If you look at my example, the src of the iframe should be: http://www.youtube.com/embed/bSiDLCf5u3s In your example the source is replaced with the same url as the input. – ShyGuy82 Apr 05 '13 at 15:12
  • @ShyGuy82, don't know why this matters, but anyhow: made an update to accomplish this requirement. – Linus Caldwell Apr 05 '13 at 16:08
  • 1
    Thanks a lot, works like a charm, I really appreciated the help. – ShyGuy82 Apr 05 '13 at 16:27
  • Sorry to bug you again. This code fails if the VIDEO ID contains underscores and dashes. For example a URL like this fails: http://www.youtube.com/watch?v=_-QpUDvTdNY Notice there is an underscore and a dash in it. – ShyGuy82 Apr 05 '13 at 23:02
  • @ShyGuy82, that's not so hard. Maybe it's a good idea to read something about regex. Just replace the part that gets the ID with `[\w\-]+`. – Linus Caldwell Apr 06 '13 at 03:33
  • Thank you very much for your help, that worked. I will read more on RegEx as it's new thing to me. – ShyGuy82 Apr 07 '13 at 20:23
  • To simplify the regex, I chose to modify `pattern` to `@"(http[s]?://)?(www\.)?(youtube.com/watch\?[^?]*v=|youtu.be/)(?[\w\-]+[^\s?]+)"`. Then in the `replacement` string, I use `${vid}` instead of `$1`. – Doug S Jul 02 '14 at 02:50
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
  }
}
Narnia
  • 1
  • 2
  • It would be great if you can add some texts and explain how your code works. That would help more So users. :) – jazzurro Dec 20 '14 at 03:51
  • `foreach (Match myMatch in myRegex.Matches(strTargetString)) { if (myMatch.Success) { string src = myMatch.Groups["SRC"].Value.ToString(); string embed = myMatch.Groups["EMBED"].Value.ToString(); } }` – Narnia Jan 13 '15 at 07:39