我想要匹配的是这个正则表达式:^[a-zA-Z]{2}[0-9]{10}$
例如,如何测试E2
的内容是否与[A-Za-z][A-Za-z]##########
类似,是否可以将其包含在IF
语句中?
答案 0 :(得分:0)
使用此:
Function regxMatch(Value As String, Pattern As String, Optional IgnoreCase As Boolean = False)
Dim r As New VBScript_RegExp_55.RegExp
r.Pattern = Pattern
r.IgnoreCase = IgnoreCase
If r.Test(Value) Then
M = "Matches '" & Pattern & "'"
Else
M = ""
End If
End Function
功能应该是自我解释的!
答案 1 :(得分:0)
您可以执行较短的测试:
Function regexTest(strIn As String, strPattern As String) As Boolean
Dim objRegex As Object
Set objRegex = CreateObject("vbscript.regexp")
objRegex.Pattern = strPattern
regexTest = objRegex.test(strIn)
End Function
测试代码
Sub TestME()
MsgBox regexTest("ZAE000006284", "^[a-zA-Z]{2}[0-9]{10}$") ' False
MsgBox regexTest("ZA0000000628", "^[a-zA-Z]{2}[0-9]{10}$") ' True
End Sub