Results 1 to 11 of 11

Thread: format.string question how to?

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Posts
    12

    format.string question how to?

    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)


    Code:
    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..thank you for your help..
    -itdaddy

  2. #2
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: format.string question how to?

    I would use Regular Expressions to handle this.

    Here's an example where I'm checking for an IP Address. (Find a better pattern 999.999.999.999 validates with this one).

    Code:
    Imports System.Text.RegularExpressions
    
    Public Class Input
    
        Public Enum InputTypes
            Invalid = -1
            IPAddress
            MACAddress
            PCName
        End Enum
    
        Private Shared InputPatterns As New List(Of String) _
            (New String() { _
                "\b(?:\d{1,3}\.){3}\d{1,3}\b", _
                "MAC Address Pattern", _
                "PC Name Pattern"})
    
        Private Shared _inputString As String
    
        Public Shared Function GetInputType(ByVal inputString As String)
            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
    Example of calling it.

    Code:
    Public Class Form1
    
        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

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Posts
    12

    Re: format.string question how to?

    hey mattp thanks so much
    will try it and let you know what I come up with..thanks so much for your guidance..

  4. #4

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Posts
    12

    Re: format.string question how to?

    mattp
    hey bud I can basically use your class right? and then incorporate it into my calling it code right?
    I mean your code looks complete except for the string pattern for mac and pcname right? I just have to
    make the reg expression for macid and pcname right and then I could use your class instantiated into my form1 right?

  5. #5
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: format.string question how to?

    I have no problem with you using the code.

    You're correct that you'll just need to add the regular expression for each pattern you want to check against. As I said you'll probably want to devise a better expression for the IP Address than the one I provided as it will return valid for impossible IP Addresses.

  6. #6

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Posts
    12

    Re: format.string question how to?

    thanks MattP yeah I was reading that it will allow 999.999.999.999
    but your structure sure helps..thanks so much for the insight

  7. #7
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: format.string question how to?

    Some better expression patterns to use.

    Code:
        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}$"})

  8. #8

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Posts
    12

    Re: format.string question how to?

    hey mattp

    you just made up the patterns. I see how wow thanks did you use the regex prgram to do that?
    and is it a good program? the unix one for windows?
    thanks

  9. #9

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Posts
    12

    Re: format.string question how to?

    HEY MATTp

    my ide visual studio 2008 express doesnt like the function getinputtype()
    for some reason? do know why? thanks it underlines it in blue sguiggly?
    saying it needs and As clause? huh?

    Code:
    Public Shared Function GetInputType(ByVal inputString As String)
            Dim InputType As InputTypes = InputTypes.Invalid
            _inputString = inputString
            InputType = CType(InputPatterns.FindIndex(AddressOf FindPattern), InputTypes)
            _inputString = ""
            Return InputType
        End Function

  10. #10
    Frenzied Member MattP's Avatar
    Join Date
    Dec 2008
    Location
    WY
    Posts
    1,227

    Re: format.string question how to?

    Code:
    Public Shared Function GetInputType(ByVal inputString As String) As InputTypes
    Well, it's because I'm lazy and didn't have Option Strict on. Visual Studio with this option on it doesn't like implicitly converting Object to InputTypes.

  11. #11

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Posts
    12

    Re: format.string question how to?

    MATTP

    what is your background can I ask and who did you get so good at vb.net?
    what other languages do you do? and can you point me to some great programming refernces to get as good as you sort of as good as you
    probably wont happen in this lifetime but can you point me to some good references you used maybe? thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width