Question Issue with Regex and IP:Port

digitaldrew

Well-known member
Joined
Nov 10, 2012
Messages
167
Programming Experience
Beginner
Hey everyone..I am trying to use a regular expression so I can pull all IP:port off a specific page and add them to a listbox, but nothing seams to be happening.

Any idea where I am going wrong here?

VB.NET:
    Private Sub btnSourceA_Click(sender As System.Object, e As System.EventArgs) Handles btnSourceA.Click
        Dim request As HttpWebRequest = Nothing
        Dim response As HttpWebResponse = Nothing
        Try

            request = DirectCast(WebRequest.Create(("http://nntime.com/proxy-list-01.htm")), HttpWebRequest)
            request.Accept = "*/*"
            request.Method = "GET"
            request.Timeout = &H2710
            request.ReadWriteTimeout = &H2710
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)"
            response = DirectCast(request.GetResponse, HttpWebResponse)
            Dim input As String = New StreamReader(response.GetResponseStream).ReadToEnd
                Dim regex2 As New Regex("\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b\:(\d+)", RegexOptions.Multiline)
            For Each match2 As Match In regex2.Matches(input)
                Dim output As String = ""
                output &= match2.Groups.Item(1).Value.Trim & "."
                output &= match2.Groups.Item(2).Value.Trim & "."
                output &= match2.Groups.Item(3).Value.Trim & "."
                output &= match2.Groups.Item(4).Value.Trim & ":"
                output &= match2.Groups.Item(5).Value.Trim
                lstProxiesToTest.Items.Add(output)
            Next

        Catch exception As Exception
            Trace.WriteLine(("Get Views ex: " & exception.Message))
        Finally
            If (Not response Is Nothing) Then
                response.Close()
            End If
        End Try
        MsgBox("Completed!")
    End Sub
 
Hi,

See if this expression is any better for you:-

VB.NET:
Dim IPAddressExpression As New Regex("\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b", RegexOptions.Multiline)

Hope that helps.

Cheers,

Ian
 
Hi,

See if this expression is any better for you:-

VB.NET:
Dim IPAddressExpression As New Regex("\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b", RegexOptions.Multiline)

Hope that helps.

Cheers,

Ian

Hey Ian..thanks for your helpful response! That one got me a little further..now something is loading in the listbox..it just isn't the ip's or ports..Attached is a screenshot

Here is what I changed
VB.NET:
            Dim IPAddressExpression As New Regex("\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b", RegexOptions.Multiline)
            'Dim regex2 As New Regex("\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b\:(\d+)", RegexOptions.Multiline)
            For Each match2 As Match In IPAddressExpression.Matches(input)
 

Attachments

  • ss1.JPG
    ss1.JPG
    10.3 KB · Views: 24
Hmmmm, that looks a bit weird? Can you post a sample of the "input" string being returned from your web request? Can you also point out where in the string a match should be made (I know it should be obvious, but just to be clear). I am sure we should be able to crack it from there.

Cheers,

Ian
 
Hey Ian..When I add the msgbox for (input) it brings up pretty much the entire page in a msgbox..see attachment.

It's hard to see where exactly the match should be made in the msgbox because I can't even see to the end of it due to the size of my laptop monitor.
 

Attachments

  • ss2.JPG
    ss2.JPG
    110 KB · Views: 25
Hi,

You need to change your For loop. Just tested your whole code and it works with the RegEx I provided if you then do:-

VB.NET:
For Each match2 As Match In IPAddressExpression.Matches(input)
  Dim output As String = match2.ToString
  'you actually do not need to interrogate the groups here
  For Each GRP In match2.Groups
    MsgBox(GRP.ToString)
  Next
  lstProxiesToTest.Items.Add(output)
Next

Hope that helps.

Cheers,

Ian
 
Hey Ian..Thanks for your time on this! You were right (as usual)..using your loop code and your regex it seams to pull them just fine now. However, it is only pulling the IP address. Is it possible to have the regex pull the ipaddress and the port as well? Would I just add \:(\d+) to the end of your current regex?

many thanks again!
 
Hi,

Actually, No. To get the Port does not seem to be an easy matter. To explain, get the web page in a browser and then right click and view the source (this is what you are downloading with your web request). Then search for any of the IP addresses on the page, say 94.23.52.82. If you look at the web page you will see that the port is 3128 but it is not associated with the IP address in the current tag. The port is actually defined as the last x characters in the "value" attribute above the IP address. Now you could write another RegEx expression to get that but you have no way of knowing how many characters make up the Port number since there does not seem to be anything to indicate how many characters to take from the end of the value attribute??

Not sure if that helps this time but at least you know what your next challenge is.

Good Luck and Cheers,

Ian
 
Hey Ian, thanks for your response. I understand what you're saying now that I go and review the source of that page.

But, what if I were to use a different URL? Say proxylistdaily . com

If I were going to try pulling them from there where the URL's are actually in ip:port format..would it work by appending the last part of the regex I listed above?

Thanks again!


UPDATE: I tried making the change myself and it appears to be working with that URL..looks like your regex works like a charm as long as they are actually listed in the right format on the website. Thanks so much Ian you're such a great help!
 
Hi,

You are welcome and you are correct the full RegEx expression, as below, works fine on that site. I have also made a small change to the For loop in case you need that too.

VB.NET:
Dim IPAddressExpression As New Regex("\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b\:(\d+)", RegexOptions.Multiline)
For Each match2 As Match In IPAddressExpression.Matches(input)
  'In this case you can use the .Split method the split the IP Address and Port
  'or you can just use Match2.ToString as is to get the IP address and Port together
  Dim Output() As String = match2.ToString.Split(":"c)
  For Each strElement In Output
    MsgBox(strElement)
  Next
  lstProxiesToTest.Items.Add(match2.ToString)
Next

Hope that helps.

Cheers,

Ian
 
Back
Top