Grab IDs with Regex?

jas92

Newbie
Joined
Jan 20, 2014
Messages
15
Reaction score
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
 
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
 
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","");
 
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
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
 
I see you're using VB.NET.

I would simply split the string, since it's much easier. Although it's not a best practice...

Check out this:
I'll have a look into that now mate, thanks.

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","");
Would this work if there's multiple multiple IDs? Sorry this is really confusing lol.
 
Yes it would. Post the whole text junk here and I will take a closer look.
 
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
 
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)
 
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)

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
 
Back
Top