使用喜欢比较一个string在Excel中
我想匹配的是这个正则expression式: ^[a-zA-Z]{2}[0-9]{10}$
例如,如何testingE2
的内容是否像[A-Za-z][A-Za-z]##########
,并且可以将它包装到IF
语句中?
用这个:
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
函数应该是自解释的!
你可以运行一个更短的testing:
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
testing代码
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