"Check" <guest@newsgroup-email.com> wrote in message
news:4624676b524437ec0fa8a6e01b1ad9e6@newsgroup-gateway.com...
>
> I have written something like this:
>
> Dim sStringToSearch()
> ReDim sStringToSearch(1)
>
> sStringToSearch(0) = "ame i"
> sStringToSearch(1) = "ti"
>
> bCaseSensitive = False
> For Each sValue In sStringToSearch
> If bCaseSensitive <> True Then
> sValue = LCase(sValue)
> End If
>
> sLogFilePath = "C:\Test\Test.txt"
> Set objFSO = CreateObject("Scripting.FileSystemObject")
> strData = objFSO.OpenTextFile(sLogFilePath & sFileName, 1).ReadAll
>
> arrLines = Split(strData, vbCrLf)
>
> For iLineCount = 0 To UBound(arrLines)
> iOccur = 0
> sString = ""
> sLineData = arrLines(iLineCount)
> If bCaseSensitive = False Then
> sLineData = LCase(sLineData)
> End If
>
> For iCount = 1 To Len(sLineData)
> If InStr(iCount, sLineData, sValue) > 0 Then
> iLocationOfString = InStr(iCount, sLineData, sValue)
> iTrailingSpace = InStrRev(sLineData, " ",
> iLocationOfString)
> If iTrailingSpace = 0 Then
> iTrailingSpace = 0
> End If
> iLeadingSpace = InStr(iLocationOfString + Len(sValue),
> sLineData, " ")
> If iLeadingSpace = 0 Then
> iLeadingSpace = Len(sLineData) + 1
> End If
> iWordLength = iLeadingSpace - iTrailingSpace - 1
>
> sWord = Mid(sLineData, iTrailingSpace + 1, iWordLength)
> Else
> Exit For
> End If
> sString = sString & "|" & sWord
> iCount = iLeadingSpace
> iOccur = iOccur + 1
> Next
> If iOccur > 0 Then
> MsgBox "String To Search: " & sValue
> MsgBox "Line Number: " & iLineCount + 1
> MsgBox "Occurance: " & iOccur
> MsgBox "Matching words found: " & sString
> End If
> Next
> Next
>
> This caters everything - string search, list of string search, case
> sensitive search.
>
> Is there any better way to implement this?
>
> Also, can anyone tell me how to implement wild card search in VBScript?
>
> Example: sGivenString = "My name is Peter. Her name is Latika"
>
> sStringToSearch = "n*e"
>
> Result should be: occurrence = 2, name
>
> if string to search = P?t
> occurrence = 1, Peter
>
>
> --
> Check I have an example VBScript program that uses the RegEx object. I use it to
analyze regular expression patterns. I documented the program with many
comments to explain how to use regular expressions. As noted, it is based on
a program in Tim Hill's "Windows 2000 Windows Script Host". I used it to
handle your examples with the following commands (the program is called
RegExpMatch.vbs):
cscript //nologo RegExpMatch.vbs "n[A-Za-z]+e" "My name is Peter. Her name
is Latika"
the output was:
Pattern: "n[A-Za-z]+e"
String: "My name is Peter. Her name is Latika"
Number of matches: 2
Match: name at: 4 (4)
Match: name at: 23 (4)
Next I used the command:
cscript //nologo RegExpMatch.vbs "P[A-Za-z]t" "My name is Peter. Her name is
Latika"
and the output was:
Pattern: "P[A-Za-z]t"
String: "My name is Peter. Her name is Latika"
Number of matches: 1
Match: Pet at: 12 (3)
The program follows:
============
' RegExpMatch.vbs
' Version 1.0
' January 8, 2010
' VBScript program to test various regular expression patterns
' and their affect on strings.
' Based on script RegExp.vbs (Listing 7.1 on page 197) from
' "Windows 2000 Windows Script Host" by Tim Hill,
' Macmillan Technical Publishing, Copyright 1999.
' Examples of patterns:
' . any character
' .. any 2 characters
' ^abc string starts wtih abc
' red$ string ends with red
' [0-9] a digit
' [0-9]* 0 or more digits
' [0-9]+ 1 or more digits
' [0-9]? 0 or 1 digit
' [0-9a-zA-Z] alphanumeric character
' [^0-9] not a digit
' [a-zA-Z][a-zA-Z_0-9]* valid variable name
' a{3} aaa
' a{3,} at least three a characters
' a{3,5} 3 to 5 a's
' [0-9]{4} 4 digits
' \d any digit, same as [0-9]
' \D any non-digit, same as [^0-9]
' \w any word character, same as
[0-9a-zA-Z_]
' \W any non-word character
' \b word boundary
' \B non-word boundary
' \s whitespace character
' \S non-whitespace character
' \f form-feed character
' \n new-line character
' \r carriage-return character
' \v vertical tab character
' \onn octal code nn
' \xnn hexadecimal code nn
' \n (n a digit) repeat previous sub expression (in
parentheses) n times
' (..)\1 any two characters repeated twice,
like abab
' ab|ac ab or ac
' ^ab|ac$ ab at start or ac at end of string
' ^(ab|ac)$ either string ab or ac
' [^=]+=.* command line arguments
' ([a-zA-Z]\w*)=(.*) command line arguments
' (\s*)([a-zA-Z]\w*)(\s*)=(.*) command line arguments, with
whitespace
' (\s*)/([a-zA-Z]\w*)((=|

(.*)|) command line switch
' ^(.*?)\1+$ any repeating pattern in a string
Option Explicit
Dim objRE, objMatches, objMatch, strPattern, strSearchString
Select Case Wscript.Arguments.Count
Case 1
strPattern = Wscript.Arguments(0)
If (strPattern = "/?") _
Or (strPattern = "-?") _
Or (strPattern = "?") _
Or (strPattern = "/H") _
Or (strPattern = "/h") _
Or (strPattern = "-H") _
Or (strPattern = "-h") _
Or (strPattern = "/help") _
Or (strPattern = "-help") Then
Call Syntax()
Wscript.Quit
End If
Case 2
strPattern = Wscript.Arguments(0)
strSearchString = Wscript.Arguments(1)
Case Else
Wscript.Echo "Wrong number of arguments"
Call Syntax()
Wscript.Quit
End Select
Wscript.Echo "Pattern: """ & strPattern & """"
Wscript.Echo "String: """ & strSearchString & """"
Set objRE = New RegExp
objRE.Pattern = strPattern
objRE.Global = True
Set objMatches = objRE.Execute(strSearchString)
Wscript.Echo "Number of matches: " & objMatches.Count
For Each objMatch In objMatches
Wscript.Echo "Match: " & objMatch.Value & " at: " _
& CStr(objMatch.FirstIndex + 1) & " (" & objMatch.Length & ")"
Next
Sub Syntax()
Wscript.Echo "Syntax:"
Wscript.Echo " cscript RegExpMatch.vbs <pattern> <string>"
Wscript.Echo "where:"
Wscript.Echo " <pattern> is a regular expression pattern"
Wscript.Echo " <string> is a string to test against the pattern"
Wscript.Echo "For example:"
Wscript.Echo " cscript RegExpMatch.vbs ""[A-Za-z0-9\+/]+[=]?[=]?""
""VGhpcyBpcyBhIHRlc3Q="""
End Sub
--
Richard Mueller
MVP Directory Services
Hilltop Lab -
http://www.rlmueller.net
--