Question

i try to extract video ID from youtube using Regex.Match, for example I have www.youtube.com/watch?v=3lqexxxCoDo and i want to extract only 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)

how i can extract video id ?, thanks !

Was it helpful?

Solution

Finally got the solution

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

OTHER TIPS

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

This function will return just the video ID.

you can basically replace "www.youtube.com/watch?v=" with "" using "String.Replace"
MSDN String.Replace

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

You can use this expression, in PHP I am using this.

function parseYtId($vid)
{
    if (preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $vid, $match)) {
        $vid = $match[1];
    }
    return $vid;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top