C# regex to get video id from youtube and vimeo by url

前端 未结 3 2007

I\'m busy trying to create two regular expressions to filter the id from youtube and vimeo video\'s. I\'ve already got the following expressions;

YouTube: (y         


        
3条回答
  •  执念已碎
    2020-12-02 14:05

    In case you are writing some application with view model (e.g. ASP.NET MVC):

    public string YouTubeUrl { get; set; }
    
    public string YouTubeVideoId
    {
        get
        {
            var youtubeMatch =
                new Regex(@"youtu(?:\.be|be\.com)/(?:.*v(?:/|=)|(?:.*/)?)([a-zA-Z0-9-_]+)")
                .Match(this.YouTubeUrl);
            return youtubeMatch.Success ? youtubeMatch.Groups[1].Value : string.Empty;
        }
    }
    

提交回复
热议问题