Skip to content

Instantly share code, notes, and snippets.

@leandrocustodio
Forked from Godoy/gist:5032291
Last active December 14, 2015 19:49
Show Gist options
  • Save leandrocustodio/5139185 to your computer and use it in GitHub Desktop.
Save leandrocustodio/5139185 to your computer and use it in GitHub Desktop.
Expressão regular em C# para pegar ID de vídeos de uma url do Youtube.
string id = string.Empty;
if (!String.IsNullOrEmpty(@Model.Video))
{
System.Text.RegularExpressions.Regex regexYoutube = new System.Text.RegularExpressions.Regex("youtu(?:\\.be|be\\.com)/(?:.*v(?:/|=)|(?:.*/)?)([a-zA-Z0-9-_]+)");
System.Text.RegularExpressions.Match youtubeMatchs = regexYoutube.Match(Model.Video);
if (youtubeMatchs.Success)
{
id = youtubeMatchs.Groups[1].Value;
}
}
string imgYoutube = "http://img.youtube.com/vi/"+id+"/1.jpg";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment