Trying to match '1234**AB', so need to escape star '*' char ie. regexp pattern, from my understanding needs to be '\*\*' but, no match.
Test code below...
Any idea, what is wrong?
Test code below...
Any idea, what is wrong?
Code:
Private Sub Form_Load()
'Regular expressions needs to be referenced in project.
'Project menu, References
'Microsoft VBScript Regular Expressions 5.5' - vbscript.dll
Barcode = "1234**3AB"
DBProductCode = "1234\*\*3AB"
If RegExpTest(Barcode, DBProductCode) Then
Debug.Print "Match"
Else
Debug.Print "No match"
End If
End Sub
Code:
Function RegExpTest(strToSearch, strSearchPattern)
Dim regEx
Dim RetVal
Dim sLength
Dim sTemp
Dim i
Dim sCH
Dim bNonNumericChars
Dim bSetStringAnchor
Dim Matches
Dim Match
Dim MatchesCount
bNonNumericChars = False
bSetStringAnchor = False
sTemp = UCase(strToSearch)
For i = Len(sTemp) To 1 Step -1
sCH = Asc(Mid(sTemp, i, 1))
If sCH >= 65 And sCH <= 92 Then 'Contains letter A-Z.
bNonNumericChars = True
If i = Len(strToSearch) Then bSetStringAnchor = True 'Last char is letter, add end of string anchor '$' to strSearchPattern
Exit For
End If
Next
RetVal = False 'Return value set to False
Set regEx = New RegExp ' Create a regular expression.
If bNonNumericChars And bSetStringAnchor Then
regEx.Pattern = strSearchPattern & "$" ' Set pattern.
Else
regEx.Pattern = strSearchPattern
End If
regEx.IgnoreCase = True ' Set Case insensitivity.
regEx.Global = True ' Set global applicability.
If bNonNumericChars Then 'Alphanumeric match
Set Matches = regEx.Execute(strToSearch) ' Execute search.
For Each Match In Matches
's = s & Match.Value & "'."
MatchesCount = Matches.Count
sLength = Match.Length
Next
If MatchesCount > 0 Then 'There were match
If Len(strToSearch) = sLength Then RetVal = True 'True if length is same, than strToSearch
End If
Else 'Numeric match
RetVal = regEx.Test(strToSearch) 'Test search.
End If
RegExpTest = RetVal 'True if match found
Set regEx = Nothing
End Function