Friday, May 13, 2011

Two Ways to Do String Matching in VBscript

The ugly but easier way (using InStr function)

Const strValue = "Microsoft Windows 7 Enterprise Edition"
If InStr(strValue, "Windows 7") > 0 Or InStr(strValue, "Windows XP") > 0 Then
wscript.echo "match found"
End If


The fancier but more irritating way (using the REGEX object):



Function MatchString(strToSearch, strPattern)
Dim objRegEx, colMatches, matchFound
Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Global = True
objRegEx.Pattern = strPattern
Set colMatches = objRegEx.Execute(strToSearch)
If colMatches.Count > 0 Then
matchFound = True
End If
Set objRegEx = Nothing
MatchString = matchFound
End Function

If MatchString(strValue, "Windows XP|Windows 7") Then
wscript.echo "match found"
End If

No comments:

Post a Comment