What I want to match is this regex: ^[a-zA-Z]{2}[0-9]{10}$
For example, how can I test whether the contents of E2
are like [A-Za-z][A-Za-z]##########
, and can I wrap that into an IF
statement?
What I want to match is this regex: ^[a-zA-Z]{2}[0-9]{10}$
For example, how can I test whether the contents of E2
are like [A-Za-z][A-Za-z]##########
, and can I wrap that into an IF
statement?
Use this:
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
Function should be self explanatory!
You can run a shorter test as such:
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
test code
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