vba rgex wildcard code example
Example: vba regex wildcard
Function RegexMatch(ByVal pSearched As String, ByVal pPattern As String, _
Optional ByVal pIgnoreCase As Boolean = True) As Boolean
Dim rRegex As Object
Set rRegex = CreateObject("VBScript.RegExp")
pPattern = Replace(pPattern, ".", "[.]")
pPattern = Replace(pPattern, "*", ".*")
pPattern = Replace(pPattern, "?", ".")
With rRegex
.IgnoreCase = pIgnoreCase
.Pattern = pPattern
End With
RegexMatch = rRegex.test(pSearched)
End Function
Sub TestMe()
Debug.Print RegexMatch("helloworld.txt", "*.txt")
Debug.Print RegexMatch("helloworld.txt", "hell*o?ld.tx*")
Debug.Print RegexMatch("HELLOworld.txt", "hel*.tx*", False)
End Sub