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

如何使用Regex.Match提取youtube视频id,regex,vb.net,vb.net-2010,matching,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(u

我尝试使用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?谢谢

您基本上可以使用“String.replace”将“www.youtube.com/watch?v=”替换为“using”


终于找到了解决办法

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

此函数将仅返回视频ID。

您可以使用此表达式,在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;
}

您可以在.NET中使用
HttpUtility.ParseQueryString()
来获取
v
查询字符串值,而不会弄乱正则表达式。类似于
HttpUtility.ParseQueryString(queryString)(“v”)
Ref:
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
function parseYtId($vid)
{
    if (preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $vid, $match)) {
        $vid = $match[1];
    }
    return $vid;
}