一.创建RegExp对象
属性:
1. IgnoreCase
设置或返回一个Boolean值,指明模式搜索是否区分大小写。如果搜索是区分大小写的,则 IgnoreCase 属性为 False;否则为 True。缺省值为 True。
2.Global
设置或返回一个 Boolean 值,该值指明在整个搜索字符串时模式是全部匹配还是只匹配第一个。如果搜索应用于整个字符串,Global 属性的值为 True,否则其值为 False。默认的设置为 True。
3.Pattern
设置或返回被搜索的正则表达式模式。
举例:
'检查是否包含字母
'test if there are Uppercase & lowercase letters
Function test_letters(Passwd)
test_letters = False
On Error Resume Next
Dim regEx
Set regEx = New RegExp
regEx.Pattern = "([a-z].*[A-Z])|([A-Z].*[a-z])"
regEx.IgnoreCase = False '注:如果为True, 不全部匹配字符
If regEx.Test(Passwd) Then
test_letters = True
End If
End Function
'检查是否包含数字和字母
'test if there is a number and a letter in the string
Function test_numbers(Passwd)
On Error Resume Next
Dim bolRet
Dim bolNum
Dim bolLet
bolRet = False
bolNum = False
bolLet = False
Dim regEx
Set regEx = New RegExp
'是否包含数字
regEx.Pattern = "\d+"
regEx.IgnoreCase = True
If regEx.Test(Passwd) Then
bolNum = True
End If
'是否包含字母
regEx.Pattern = "[a-z].*"
regEx.IgnoreCase = False
If regEx.Test(Passwd) Then
bolLet = True
End If
If bolNum And bolLet Then
bolRet = True
End If
test_numbers = bolRet
End Function
'检查是否包含重复字符(例如aaaa,bbbb,cccc,1111,2222)
'test for repeated chars (aaaa, bbbb, 1111, 2222)
Function test_repeatchar(Passwd)
test_repeatchar = False
Dim str_chars
str_chars = "abcdefghijklmnopqrstuvwxyz012345678909876543210zyxwvutsrqponmlkjihgfedcba"
Dim n
Dim char
Dim cmd
Dim regEx
Dim match, matchs, strmatch
Dim tt
For n = 1 To Len(str_chars)
char = Mid(str_chars, n, 1)
cmd = "[" & char & "]+"
Set regEx = New RegExp
regEx.IgnoreCase = True
regEx.global = true
regEx.Pattern = cmd
If regEx.Test(Passwd) Then
Set matchs = regEx.Execute(Passwd) 'Execute 方法返回一个 Matches 集合,其中包含了在
string 中找到的每一个匹配的 Match 对象。
如果未找到匹配,Execute 将返回空的 Matches 集合。
For Each match in matchs '循环所有匹配项
strmatch = match.value
If Len(strmatch) > 3 Then
test_repeatchar = True
Exit Function
End If
Next
End If
Next
End Function
'test for ordered chars in the string (1234, abcd, lmno, 5678)
Function test_order(Passwd)
test_order = False
On Error Resume Next
Dim str_chars
str_chars = "abcdefghijklmnopqrstuvwxyzyxwvutsrqponmlkjihgfedcba"
Dim n
Dim nc
Dim cmd
Dim regEx
Set regEx = New RegExp
regEx.IgnoreCase = True
For n = 1 To Len(str_chars)
nc = mid(str_chars, n+1, 3)
If Len(nc) = 3 Then
'Regexs in cmd should look like this after eval:
' /a(?=bcd)/i
' /b(?=cde)/i
' /c(?=def)/i
cmd = mid(str_chars,n,1) & "(?=" & nc & ")"
regEx.Pattern = cmd
If regEx.Test(Passwd) Then
test_order = True
Exit Function
End If
End If
Next
str_chars = "012345678909876543210"
For n = 1 To Len(str_chars)
nc = mid(str_chars, n+1, 3)
If Len(nc) = 3 Then
'Regexs in cmd should look like this after eval:
' /1(?=234)/i
' /2(?=345)/i
' /3(?=456)/i
cmd = mid(str_chars,n,1) & "(?=" & nc & ")"
regEx.Pattern = cmd
If regEx.Test(Passwd) Then
test_order = True
Exit Function
End If
End If
Next
End Function
'***************************************
Function RegExpTest(patrn, strng)
Dim regEx, Match, Matches ' 建立变量。
Set regEx = New RegExp ' 建立正则表达式。
regEx.Pattern = patrn ' 设置模式。
regEx.IgnoreCase = True ' 设置是否区分大小写。
regEx.Global = True ' 设置全局替换。
Set Matches = regEx.Execute(strng) ' 执行搜索。
msgbox matches.count
For Each Match in Matches ' 遍历 Matches 集合。
RetStr = RetStr & "Match " & I & " found at position "
RetStr = RetStr & Match.FirstIndex & ". Match Value is "'
RetStr = RetStr & Match.Value & "'." & vbCRLF
Next
RegExpTest = RetStr
End Function
MsgBox(RegExpTest("is.", "IS1 is2 IS3 is4"))