Skip to content

Instantly share code, notes, and snippets.

@Godoy
Last active December 14, 2015 04:59
Show Gist options
  • Save Godoy/5032291 to your computer and use it in GitHub Desktop.
Save Godoy/5032291 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