2014-03-31 20 views
0

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

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?谢谢!

+2

您可以在.NET中使用'HttpUtility.ParseQueryString()'来获取'v'查询字符串值而不会与正则表达式混淆。类似于'HttpUtility.ParseQueryString(queryString)(“v”)'Ref:http://msdn.microsoft.com/en-us/library/ms150046(v=vs.110).aspx – mafafu

回答

1

终于得到了解决

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

你基本上可以取代 “www.youtube.com/watch?v=” 用 “” 用 “与string.replace”
MSDN String.Replace

url.Replace("www.youtube.com/watch?v=","") 
0
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。

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; 
} 
相关问题