我在MS下载的RegexTester中测试是正常的,可VB中试了下没通过,有空时要研究下 VB中这样可以,只是不知道符合不符合你所有情况: '引用 Microsoft VBScript Regular ExpressionsFunction TestRegExp(s As String, p As String) As String
Dim oRegExp As RegExp Dim oMatch As Match Dim colMatches As MatchCollection Dim RetStr As String Set oRegExp = New RegExp oRegExp.Pattern = p oRegExp.IgnoreCase = True oRegExp.Global = True If oRegExp.Test(s) Then Set colMatches = oRegExp.Execute(s) For Each oMatch In colMatches RetStr = RetStr & oMatch.Value & vbCrLf Next Else RetStr = "String Matching Failed" End If TestRegExp = RetStr End FunctionPrivate Sub Command1_Click() Dim s As String Dim p As String
s = "滚滚长江东逝水,浪花淘尽(英雄)。" _ & "是非成败转头空。(青山)依旧在,几度夕阳红。" _ & "白发渔樵江渚上,惯看秋月春风。一壶浊酒喜相逢。" _ & "(古今)多少事,都付笑谈中?" s = StrConv(s, vbNarrow) p = "\(\S{2}\)" Debug.Print TestRegExp(s, p) End Sub
if str= "英雄" or str="青山 " or str="古今"
VB中这样可以,只是不知道符合不符合你所有情况:
'引用 Microsoft VBScript Regular ExpressionsFunction TestRegExp(s As String, p As String) As String
Dim oRegExp As RegExp
Dim oMatch As Match
Dim colMatches As MatchCollection
Dim RetStr As String Set oRegExp = New RegExp oRegExp.Pattern = p
oRegExp.IgnoreCase = True
oRegExp.Global = True If oRegExp.Test(s) Then
Set colMatches = oRegExp.Execute(s)
For Each oMatch In colMatches
RetStr = RetStr & oMatch.Value & vbCrLf
Next
Else
RetStr = "String Matching Failed"
End If
TestRegExp = RetStr
End FunctionPrivate Sub Command1_Click()
Dim s As String
Dim p As String
s = "滚滚长江东逝水,浪花淘尽(英雄)。" _
& "是非成败转头空。(青山)依旧在,几度夕阳红。" _
& "白发渔樵江渚上,惯看秋月春风。一壶浊酒喜相逢。" _
& "(古今)多少事,都付笑谈中?"
s = StrConv(s, vbNarrow)
p = "\(\S{2}\)"
Debug.Print TestRegExp(s, p)
End Sub