如何使用Regex.Match提取youtube视频ID

时间:2014-03-31 19:21:56

标签: regex vb.net vb.net-2010 matching

我尝试使用Regex.Match从youtube中提取视频ID,例如我有www.youtube.com/watch?v=3lqexxxCoDo,我想只提取3lqexxxCoDo。

    Dim link_vids As Match = Regex.Match(url_comments.Text, "https://www.youtube.com/watch?v=(.*?)$")

    url_v = link_vids.Value.ToString
    MessageBox.Show(url_v)

我如何提取视频ID?,谢谢!

4 个答案:

答案 0 :(得分:1)

终于得到了解决方案

Dim Str() As String
        Str = url_comments.Text.Split("=")
        url_v = Str(1)

答案 1 :(得分:1)

Private Function getID(url as String) as String
    Try
        Dim myMatches As System.Text.RegularExpressions.Match 'Varible to hold the match
        Dim MyRegEx As New System.Text.RegularExpressions.Regex("youtu(?:\.be|be\.com)/(?:.*v(?:/|=)|(?:.*/)?)([a-zA-Z0-9-_]+)", RegexOptions.IgnoreCase) 'This is where the magic happens/SHOULD work on all normal youtube links including youtu.be
        myMatches = MyRegEx.Match(url)
        If myMatches.Success = true then
            Return myMatches.Groups(1).Value
        Else
            Return "" 'Didn't match something went wrong
        End If
    Catch ex As Exception
        Return ex.ToString
    End Try
End Function

此功能仅返回视频ID。

答案 2 :(得分:0)

你基本上可以替换" www.youtube.com/watch?v ="用""使用" String.Replace"
MSDN String.Replace

url.Replace("www.youtube.com/watch?v=","")

答案 3 :(得分:0)

你可以在PHP中使用这个表达式我正在使用它。

function parseYtId($vid)
{
    if (preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $vid, $match)) {
        $vid = $match[1];
    }
    return $vid;
}