正则表达式为:[\s\w:.]+(?=\<\/li\b)
要搜索的字符串是:
<li>aaaaa</li><li>bbbb</li><li>cccccccccc</li>
在线测试的时候查询没有问题,可以查三个结果,而且在Dreamweaver里面测试也没有问题,但是用VB的是时候就不行了,没有匹配的结果,以下是函数:Public Function RegExp(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 = "String Matching Failed"
End If
RegExp = RetStr
End Function
要搜索的字符串是:
<li>aaaaa</li><li>bbbb</li><li>cccccccccc</li>
在线测试的时候查询没有问题,可以查三个结果,而且在Dreamweaver里面测试也没有问题,但是用VB的是时候就不行了,没有匹配的结果,以下是函数:Public Function RegExp(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 = "String Matching Failed"
End If
RegExp = RetStr
End Function
引用Microsoft VBScript Regular Expressions 5.5Function bTest(ByVal s As String, ByVal p As String) As Boolean
Dim re As RegExp
Set re = New RegExp
re.IgnoreCase = False '设置是否匹配大小写
re.Pattern = p
bTest = re.Test(s)
End FunctionPrivate Sub Command1_Click() Dim s As String
Dim p As String
s = " 。欢迎致电!" '测试字符串中是否包含数字:
p = "\d+"
MsgBox bTest(s, p) '测试字符串中是否全是由数字组成:
p = "^\d+$"
MsgBox bTest(s, p) '测试字符串中是否有大写字母:
p = "[A-Z]+"
MsgBox bTest(s, p)
End Sub