1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Grab IDs with Regex?

Discussion in 'Visual Basic .NET' started by jas92, Feb 11, 2014.

  1. jas92

    jas92 Newbie

    Joined:
    Jan 20, 2014
    Messages:
    15
    Likes Received:
    0
    Hello again :D I've only learned about Regex recently, when I say that I mean since yesterday lol. Anyway, I'm trying to grab video IDs from a website, and I can get it to grab the whole of it like so: "video0000/0/hello_there" but I want to grab from here: vid"eo0000/0/hello_there" but I can't get it to work :/
    Can anyone see where I'm going wrong?
    Code:
    Dim vidurl As New System.Text.RegularExpressions.Regex("<p><a href=""/vid.*/0/.*"">")
            Dim matches As MatchCollection = vidurl.Matches(vidid)
    
            For Each videoid As Match In matches
                ListBox1.Items.Add(videoid.Value.Split("""").GetValue(1))
    
    If you need to see anymore of the code, let me know, thanks :D
     
  2. BlueZero

    BlueZero Power Member

    Joined:
    Jul 6, 2011
    Messages:
    500
    Likes Received:
    257
    Occupation:
    Webdeveloper, Project Manager
    Location:
    Byte in the Net
    Home Page:
    It is not really clear what part you want. Suppose the link is:
    video0000/0/hello_there
    then this regex:
    video(.*?)\/(.*?)\/(.*)
    gives you 3 parts, grab which one you want from them :p
     
    • Thanks Thanks x 1
  3. negligence

    negligence Regular Member

    Joined:
    Jan 3, 2010
    Messages:
    256
    Likes Received:
    331
    • Thanks Thanks x 1
  4. Macthetrix

    Macthetrix Regular Member

    Joined:
    Feb 21, 2013
    Messages:
    379
    Likes Received:
    390
    Occupation:
    The Boss
    Location:
    Silicon Valley
    not exactly sure what you mean but if you can grab "video0000/0/hello_there" and want to have vid"eo0000/0/hello_there" just use replace.

    string x = "video0000/0/hello_there";
    string y = x.Replace("vid","");
     
    • Thanks Thanks x 1
  5. jas92

    jas92 Newbie

    Joined:
    Jan 20, 2014
    Messages:
    15
    Likes Received:
    0
    Sorry bro, I'm trying to get the "eo0000/0/hello_there" from "video0000/0/hello_there"


    This:
    Code:
    "<p><a href=""/vid.*/0/.*"">"
    Will bring "video0000/0/hello_there" into my listbox, but I'm trying to get it to bring from "eo0000/0/hello_there" to my listbox if that makes more sense? lol
     
  6. jas92

    jas92 Newbie

    Joined:
    Jan 20, 2014
    Messages:
    15
    Likes Received:
    0
    I'll have a look into that now mate, thanks.

    Would this work if there's multiple multiple IDs? Sorry this is really confusing lol.
     
  7. Macthetrix

    Macthetrix Regular Member

    Joined:
    Feb 21, 2013
    Messages:
    379
    Likes Received:
    390
    Occupation:
    The Boss
    Location:
    Silicon Valley
    Yes it would. Post the whole text junk here and I will take a closer look.
     
  8. inaga

    inaga Newbie

    Joined:
    Feb 4, 2010
    Messages:
    12
    Likes Received:
    3
    There are quite a number of ways to eliminate 3 characters from a string in VB.NET, here are a few I know of ( some may not apply to your project ):

    Loop Method:

    Code:
    Dim i As Integer = 3
    Dim x As String = "video0000/0/hello_there"
    Dim y As String = ""
    Do Until i = Len(x)
        y += x(i)
        i += 1
    Loop
    
    For Method:

    Code:
    Dim i as Integer = 0
    Dim x As String = "video0000/0/hello_there"
    Dim y As String = ""
    For i = 3 To Len(x) - 1
        y += x(i)
    Next i
    
    Remove() Method:

    Code:
    Dim x As String = "video0000/0/hello_there"
    Dim y As String = x.Remove(0, 3)
    
    Replace() Method:

    Code:
    Dim x As String = "video0000/0/hello_there"
    Dim y As String = x.Replace("vid", "")
    
    Split() Method:

    Code:
    Dim x As String = "video0000/0/hello_there"
    Dim y As String = Split(x, "vid")(1)
    
    Split() Method 2:

    Code:
    Dim x As String = "video0000/0/hello_there"
    Dim y As String = x.Split(New String() {"vid"}, StringSplitOptions.None)(1)
    
    Substring() Method:

    Code:
    Dim x As String = "video0000/0/hello_there"
    Dim y As String = x.Substring(3)
    
    Mid() Method:

    Code:
    Dim x As String = "video0000/0/hello_there"
    Dim y As String = Mid(x, 4, Len(x) - 3)
    
    Trim() Method:

    Code:
    Dim x As String = "video0000/0/hello_there"
    Dim y As String = x.Trim(New Char() {"v", "i", "d"})
    
    TrimStart() Method:

    Code:
    Dim x As String = "video0000/0/hello_there"
    Dim y As String = x.TrimStart(New Char() {"v", "i", "d"})
    
    ToArray() Method:

    Code:
    Dim i as Integer = 0
    Dim x As String = "video0000/0/hello_there"
    Dim y As String = ""
    For i = 3 To x.Length - 1
        y += x.ToArray(i)
    Next i
    
     
  9. artizhay

    artizhay BANNED BANNED

    Joined:
    Nov 21, 2010
    Messages:
    1,867
    Likes Received:
    1,335
    Why not regex as requested?

    Code:
    Dim matchPattern As String = "^vid" 
    Dim matchReplacement As String = "" 
    Dim regexMachine As New Regex(matchPattern) 
    Dim result As String = regexMachine.Replace(videoId, matchReplacement)
     
  10. inaga

    inaga Newbie

    Joined:
    Feb 4, 2010
    Messages:
    12
    Likes Received:
    3
    I suppose the only answer to that I can give is none of the examples I shared require any new references/imports to be added to a default VB.NET application. Not sure how much of a benefit or loss that may be. Just my $0.02