Welcome to RegexAdvice Sign in | Join | Help

Pranav Ainavolu

VSTO, COM and Outlook
Check for valid URL using Regular Expressions

Here is the code to check whether the given string is a valid URL or not using Regular Expresssions.

Public Function IsValidUrl(ByVal Url As String) As Boolean
       Dim strRegex As String = "^(https?://)" _
                                 & "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" _
                                 & "(([0-9]{1,3}\.){3}[0-9]{1,3}" _
                                 & "|" _
                                 & "([0-9a-z_!~*'()-]+\.)*" _
                                 & "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\." _
                                 & "[a-z]{2,6})" _
                                 & "(:[0-9]{1,4})?" _
                                 & "((/?)|" _
                                 & "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$"

        Dim re As RegularExpressions.Regex = New RegularExpressions.Regex(strRegex)
        MessageBox.Show("IP: " & Net.IPAddress.TryParse(Url, Nothing))
        If re.IsMatch(Url) Then
            Return True
        Else
            Return False
        End If
End Function

Hope this helps!

Posted: Monday, May 31, 2010 3:31 AM by pranavaa
Filed under: , ,

Comments

yaki said:

Hmmm ... I'm not sure about your solution. For me the code bellow is not valid url, but I can be wrong. Please, test with your regex: http://j.-.s.co/~.~~~~~?#.??%&?/blabla.e/d.e?#?#? cheers, yaki
# June 19, 2010 12:53 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

Enter the code you see below

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS