Question format.string question how to?

itdaddy

New member
Joined
Mar 26, 2010
Messages
4
Programming Experience
1-3
Hello everyone

I am trying to figure out how to use the format.string() function
This is what I have. I have 3 data string inputs of the format as folllows:

pcname bigservername (maybe 20 character max)
ip address ###.###.###.### (ex: 192.168.1.10)
macid ##-##-##-##-##-## hex values for # (ex: 00-23-AF-33-BC-CE)
also the character in above can very on the Macid and the ip address
no filler zeros are used it could only be 1 digit and not 3 (###) or
it could be 3 digits ### patter but a max of 3 and min of 1 in the ip address?

I am trying to run a If Then Else statment using this format.string()
so I can filter the type of search string inputted so depending on the
type of format used in the search string, the data selects a cert display code.

I just need to figure out how to use the fomat.string() for an input box
that takes a string value. so maybe I should have the ip address be a number? and the hex a number and the pc name a string with 20 characters max? I was thinking this but I do not know how nor can I find anything on how to set up say this: (my pseudo code)

VB.NET:
If format.string({"###.###.###.###", txt_searchbox.text) then

  goto displayIPAddress()

End If

If format.string({"##-##-##-##-##-##"}, txt_searchbox.text) then

 goto displayMacID()

End If
If format.string("cccccccccccccccccccc", txt_searchbox.text) then

goto displayPCname()

End If

I know this is crued but I just dont know how to make the input format string work in vb.net and to find what I need on the NET is daunting..
Can you point me in the right direction..

thanks. All I need is to understand how to do the above code but real code
not my crap above..thanks
-itdaddy;)
 
I think Regex might help here, if your needing to evaluate the string before proceding. The pattern for the IP address is:

(\d{3}|\d{2}|\d{1}).(\d{3}|\d{2}|\d{1}).(\d{3}|\d{2}|\d{1}).(\d{3}|\d{2}|\d{1})

Let me know if this sounds right and I can help you set it up.
 
Last edited:
So what you want to do is make sure what the user types into the input box matches a pattern (ip address, mac address, etc)?

System.Text.RegularExpression is very handy for these things. Here's an example for the ip address: Regular Expression Examples (4th one down)
 
wow thanks guys. reg expressions wow they seem hard never been taught them or any good teachings will try though.

but yeah you are right the input values can be one of three types

ip address
mac address
20 character pcname

and depending on what the input pattern it shoots the data to a data output module...will try and will post what I find thanks guys!

sorry about double post but i wasn't sure where to post this
 
Answer I gave him on another board: VBForums - View Single Post - format.string question how to?

This

VB.NET:
(\d{3}|\d{2}|\d{1}).(\d{3}|\d{2}|\d{1}).(\d{3}|\d{ 2}|\d{1}).(\d{3}|\d{2}|\d{1})

Can be shortened to

VB.NET:
^(?:\d{1,3}\.){3}\d{1,3}$

But you allow for illegal IP Addresses such as 999.999.999.999

If you want to only allow valid IP Addresses you can use something like

VB.NET:
^(?:(?: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]?)$

An expression that could be used for MAC addresses is

VB.NET:
^([0-9a-fA-F][0-9a-fA-F](:|-)){5}([0-9a-fA-F][0-9a-fA-F])$

Which will match either the : or - separators.

Matching a PC name is easy assuming that you just want alphanumeric characters.

VB.NET:
^[0-9a-zA-Z]{1,20}$

Edit: quote tags allowed for emoticons in regex pattern.
 
Last edited:
Thanks MattP, I'm not the best at regex, I was just happy I could figure a way to do it.
 
I'm not the best at regex either. I'm sure someone can come along and provide better examples.

The fun part of that one is matching up the pattern to an Enum by index so you get a nice readable result.
 
Reg Expression of IP address, Mac address, pcname complete

Hello all the people who helped me. here is the finished product.
I have a few more things to finish on this but it works exactly how I need it to. again thank you for your help. you all rock! This code works no errors.
Thank you!
VB.NET:
Imports System.IO
Imports System.Text.RegularExpressions
Public Class Form1
    '*************************************************
    '   Global variable so search button can use it
    '*************************************************
    Public count As Integer = 0
    Public aryTextFile() As String
    Public signal As Integer = 0
    Public readtext As String = " "
    '*****************************
    '   Menu Drop down Section
    '*****************************
    '$$ File Open $$
    Private Sub men_FileOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles men_FileOpen.Click

        'read file line into a giant string from openfile dialog 
        dia_OpenFile.ShowDialog()

        'if signal is 1 then open file has taken place 
        signal = 1

        Dim FILE_NAME As String = dia_OpenFile.FileName.ToString()

        'creating instance of the StreamReader class called objReader
        If System.IO.File.Exists(FILE_NAME) = True Then
            Dim objReader As New System.IO.StreamReader(FILE_NAME)
            readtext = objReader.ReadToEnd
            objReader.Close()

        Else
            MsgBox("File Does Not Exist")
        End If

    End Sub
    ' $$ Exit Click &&
    Private Sub men_Exit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles men_Exit.Click
        Me.Close()
    End Sub
    ' $$ About   $$
    Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click
        AboutBox1.ShowDialog()

    End Sub
    '***************************
    '   Search Button Section
    '***************************

    '  $$ Search Button $$

    Private Sub btn_Search_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Search.Click
        If txt_EnterSearch.Text = "" Or signal = 0 Or ((rad_IPAddress.Checked + rad_MacID.Checked + rad_PCname.Checked) = 0) Then
            If txt_EnterSearch.Text = "" Then
                Val_ErrorCondition.SetError(txt_EnterSearch, "Cannot Leave Blank.")
                MsgBox("Search String Cannot be Empty!")
            End If
            If signal = 0 Then
                Val_ErrorCondition.SetError(txt_EnterSearch, "Cannot Leave Blank.")
                MsgBox("Must Open and Select a networkids.txt file!")
            End If
            If (rad_IPAddress.Checked = False) And (rad_MacID.Checked = False) And (rad_PCname.Checked = False) Then
                MsgBox("Please Set Search By radio button!")
            End If
        End If
        'reads a string and converts into array elements
        Dim i As Integer

        'created array used below
        aryTextFile = readtext.Split(CChar(","))
        'verify string input matches Search By radio button selection

        ' Loop search to find match string to imported array
        Dim bopin As Boolean

        For i = 0 To (UBound(aryTextFile) - 1)

            ' find match here.
            If txt_EnterSearch.Text.ToLower = aryTextFile(i).Trim.ToLower Then

                grp_SearchType.BringToFront()

                bopin = True
                count = i
            End If
        Next i
        If bopin = False Then
            MsgBox("Match not found!")
            Val_ErrorCondition.SetError(txt_EnterSearch, "Match Not Found!")
            txt_EnterSearch.Focus()
        End If

        Val_ErrorCondition.Clear()

    End Sub
    ' $$  NewSearch Button $$

    Private Sub btn_NewSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_NewSearch.Click
        txt_EnterSearch.Clear()
        dis_IPAddress.Clear()
        dis_MacID.Clear()
        dis_PCname.Clear()
        Val_ErrorCondition.Clear()
        rad_IPAddress.Checked = False
        rad_MacID.Checked = False
        rad_PCname.Checked = False
        txt_EnterSearch.Focus()
    End Sub
    ' $$ Exit Buttons  $$ 
    Private Sub btn_Exit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Exit.Click
        Me.Close()
    End Sub
    '***********************************
    '    Display Network IDs
    '***********************************
    Private Sub grp_SearchType_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles grp_SearchType.Enter

        '**** Display Network IDs found PCname, IP Address, MacID ****
        If rad_PCname.Checked = True Then
            'display

            dis_IPAddress.Text = aryTextFile(count - 1).Trim
            dis_MacID.Text = aryTextFile(count + 1).Trim
            dis_PCname.Text = aryTextFile(count).Trim
            MsgBox("Match Found!")


        End If

        If rad_IPAddress.Checked = True Then
            'display 
            dis_IPAddress.Text = aryTextFile(count).Trim
            dis_MacID.Text = aryTextFile(count + 2).Trim
            dis_PCname.Text = aryTextFile(count + 1).Trim
            MsgBox("Match Found!")


        End If

        If rad_MacID.Checked = True Then
            'display
            dis_IPAddress.Text = aryTextFile(count - 2).Trim
            dis_MacID.Text = aryTextFile(count).Trim
            dis_PCname.Text = aryTextFile(count - 1).Trim
            MsgBox("Match Found!")


        End If


    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim theInputString As String = "192.168.1.10"
        Dim InputType As Input.InputTypes = _
            Input.GetInputType(theInputString)
        Select Case InputType
            Case Input.InputTypes.Invalid
                MessageBox.Show("Invalid Entry")
            Case Input.InputTypes.IPAddress
                MessageBox.Show("Valid IP Address")
            Case Input.InputTypes.MACAddress
                MessageBox.Show("Valid MAC Address")
            Case Input.InputTypes.PCName
                MessageBox.Show("Valid PC Name")
        End Select

    End Sub

End Class
'****************************************************************
'    Filter to Display Class for PCname, IP Address, and MacID
'****************************************************************
Public Class Input

    Public Enum InputTypes
        Invalid = -1
        IPAddress
        MACAddress
        PCName
    End Enum

    Private Shared InputPatterns As New List(Of String) _
        (New String() { _
            "^(?:(?: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]?)$", _
            "^([0-9a-fA-F][0-9a-fA-F](:|-)){5}([0-9a-fA-F][0-9a-fA-F])$", _
            "^[0-9a-zA-Z]{1,20}$"})
    Private Shared _inputString As String
    Public Shared Function GetInputType(ByVal inputString As String) As InputTypes
        Dim InputType As InputTypes = InputTypes.Invalid
        _inputString = inputString
        InputType = CType(InputPatterns.FindIndex(AddressOf FindPattern), InputTypes)
        _inputString = ""
        Return InputType
    End Function
    Private Shared Function FindPattern(ByVal value As String) As Boolean
        Return Regex.IsMatch(_inputString, value)
    End Function
End Class
'*************************
'  End 
'*************************
 
Last edited by a moderator:
Back
Top