Using RegEx to validate a Url in VB6

Using RegEx to validate a Url in VB6

The following function is used to validate the given url using regular expressions in visual basic 6. 

Function RegExpTest(myPattern As String, myString As String)
   ‘Create objects.
   Dim objRegExp As RegExp
   Dim objMatch As Match
   Dim colMatches   As MatchCollection
   Dim RetStr As String
  
   ‘ Create a regular expression object.
   Set objRegExp = New RegExp

   ‘Set the pattern by using the Pattern property.
   objRegExp.pattern = myPattern

   ‘ Set Case Insensitivity.
   objRegExp.IgnoreCase = True

   ‘Set global applicability.
   objRegExp.Global = True

   ‘Test whether the String can be compared.
   If (objRegExp.Test(myString) = True) Then

   ‘Get the matches.
    Set colMatches = objRegExp.Execute(myString)   ‘ Execute search.

    For Each objMatch In colMatches   ‘ Iterate Matches collection.
      RetStr = RetStr & “Match found at position ”
      RetStr = RetStr & objMatch.FirstIndex & “. Match Value is ‘”
      RetStr = RetStr & objMatch.Value & “‘.” & vbCrLf
    Next
   
   Else
    RetStr = “The given string does not match the pattern”
   End If
   RegExpTest = RetStr
End Function

Calling the function in the command click event by passing the required parameters. The first parameter is the regex pattern and the second parameter is the url string to validate.

Private Sub Command1_Click()
 Dim url_test As String
 url_test = TestRegExp(”^http\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?$”, “
http://chrisranjana.com“)
 MsgBox(url_test)
End Sub

If the url is valid the function will return the match position and the match value or else it will return the failure message as specified in the else part in that function.

Leave a Reply

You must be logged in to post a comment.


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.