Friday, September 30, 2011

How to parse a IP address from a string using VB.NET

This code is simple. Have a textbox1 with a traceroute for example pasted into it. Click the button and show the IP address for each line. I use this to match IP addresses to a list so it will (when finished) translate the IP addresses to names. I do not have a DNS lookup which would be much easier but the layers of security to do a DNS lookup might make the process more complicated than it needs to be.

Imports System.Text.RegularExpressions

Public Class Form1

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

Dim line, As String
       
        Dim rx As New Regex("\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b", RegexOptions.Compiled Or RegexOptions.IgnoreCase)
       
        For i as integer = 0 To TextBox1.Lines.Count - 1
            ' Read one line from file
            line = TextBox1.Lines(i)
            Dim match As Match = rx.Match(line)
            If match.Success Then
                MsgBox(match.Value)
            End If
        Next

 End Sub

End Class

No comments: