locked
Can someone convert this vb-script to vb.net? RRS feed

  • Question

  • User-642806341 posted

    Hi, i am wondering if someone can convert this vb script code to vb.net:

    function Check_Postcode (byRef strPostcode)

      Dim strPostcodeRegExp(6)   ' holds the regular expressions for valid postcodes
      Dim intCount               ' For loop counter
      Dim strPostcodeCopy        ' Copy of postcode
     
      ' Variables used to hold regular expression object  
      Dim objRegExp, objMatches, objMatch
     
      ' Variables to hold list of valid letterrs for various parts of the post code.
      Dim strAlpha1, strAlpha2, strAlpha3, strAlpha4, strAlpha5
     
      ' Set up letters valid in thge various postcode positions
      strAlpha1 = "[abcdefghijklmnoprstuwyz]"                          ' Character 1
      strAlpha2 = "[abcdefghklmnopqrstuvwxy]"                          ' Character 2
      strAlpha3 = "[abcdefghjkstuw]"                                   ' Character 3
      strAlpha4 = "[abehmnprvwxy]"                                     ' Character 4
      strAlpha5 = "[abdefghjlnpqrstuwxyz]"                             ' Character 5
     
      ' Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA
      strPostcodeRegExp(0) = "^(" + strAlpha1 + "{1}" + strAlpha2 + "{0,1}[0-9]{1,2})([0-9]{1}" + strAlpha5 + "{2})$"

      ' Expression for postcodes: ANA NAA
      strPostcodeRegExp(1) = "^(" + strAlpha1 + "{1}[0-9]{1}" + strAlpha3 + "{1})([0-9]{1}" + strAlpha5 + "{2})$"

      ' Expression for postcodes: AANA  NAA
      strPostcodeRegExp(2) = "^(" + strAlpha1 + "{1}" + strAlpha2 + "[0-9]{1}" + strAlpha4 + "{1})([0-9]{1}" + strAlpha5 + "{2})$"
     
      ' Exception for the special postcode GIR 0AA
      strPostcodeRegExp(3) = "^(gir)(0aa)$"
     
      ' Standard BFPO numbers
      strPostcodeRegExp(4) = "^(bfpo)([0-9]{1,4})$"
     
      ' c/o BFPO numbers
      strPostcodeRegExp(5) = "^(bfpo)(c\/o[0-9]{1,3})$"
     
      ' Overseas territories
      strPostcodeRegExp(6) = "^([A-Z]{4})(1ZZ)$"

      ' Copy the parameter and convert into lowercase
      strPostcodeCopy = Lcase(strPostCode)
     
      ' Assume we're not going to find a valid postcode
      Check_Postcode = false
     
      ' Strip out spaces
      strPostcodeCopy = Replace (strPostcodeCopy, " ", "")
      Check_Postcode = False
     
      Set objRegExp = New RegExp
     
      ' Check the string against valid types of post codes
      For intCount = 0 to Ubound(strPostCodeRegExp)
     
        ' Check next pattern in list
        objRegExp.Pattern =  strPostcodeRegExp(intCount)
        If objRegExp.Test (strPostcodeCopy) Then
       
          ' Post code found. Ensure input parameter is in correct format.
          Set objMatches = objRegExp.Execute (strPostcodeCopy)
          Set objMatch = objMatches(0)
          strPostcodeCopy = Ucase (objMatch.subMatches (0)) & " " &  Ucase (objMatch.subMatches (1))
         
          ' Take account of the special BFPO c/o format
          strPostcodeCopy = Replace (strPostcodeCopy, "C/O", "c/o ")
         
          ' Show that we have found the postcode
          Check_Postcode = True
        End if
      Next
     
      ' Ensure that the uppercase postcode gets returned if valid
      If Check_Postcode Then strPostcode = strPostcodeCopy
     
    End Function

     

    Thursday, October 1, 2009 12:36 PM

Answers