现在的位置: 首页 > 综合 > 正文

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

2013年12月10日 ⁄ 综合 ⁄ 共 919字 ⁄ 字号 评论关闭

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

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

抱歉!评论已关闭.