你的位置: iPc 首页 > 全部文章 > 编程技术 > 阅读文章
科学X网    Office    苹果中国    微软中国    VPS

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

26
四月

VB中用正则表达式判断字符串是否为一个URL地址
VB6引用:Microsoft VBScript Regular Expressions 5.5
[codes=vb]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 [/codes]

VB.Net:
[codes=vb.net]Public Shared Function IsUrl(ByVal strTmp As String) As Boolean
On Error GoTo Z
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[/codes]
呵呵,确实是非常的方便实用!!

关于本文

微博评论箱