vbs字符串正则_(原创)vbs正则表达式学习

'检查是否包含字母

'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"))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值