VB中用正则表达式判断一个字符串是不是一个URL地址

来源:互联网 发布:淘宝直播账号怎么申请 编辑:程序博客网 时间:2024/06/07 06:29

VB6引用:Microsoft VBScript Regular Expressions 5.5

Public Function IsUrl(ByVal strTmp As String) As Boolean
    On Error GoTo Z
    Dim objIntPattern
    IsUrl = False
    Set objIntPattern = New RegExp
    objIntPattern.Pattern =  "^(http://|https://){0,1}[A-Za-z0-9][A-Za-z0-9/-/.]+[A-Za-z0-9]/.[A-Za-z]{2,}[/43-/176]*$"
    objIntPattern.Global = True 
    IsUrl = objIntPattern.Test(strTmp) 
    Set objIntPattern = Nothing
Z:
End Function

Private Sub Command1_Click() 
    MsgBox IsUrl( "http://www.sohu.com")
End Sub

VB.Net:

Public Shared Function IsUrl(ByVal strTmp As String) As Boolean
        On Error GoTo
        Dim objIntPattern As New System.Text.RegularExpressions.Regex( "^(http://|https://){0,1}[A-Za-z0-9][A-Za-z0-9/-/.]+[A-Za-z0-9]/.[A-Za-z]{2,}[/43-/176]*$") 
        Return objIntPattern.IsMatch(strTmp)
Z: 
End Function 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
        MsgBox (IsUrl( "http://www.sohu.com")) 
End Sub

呵呵,确实是非常的方便实用!!


原创粉丝点击