VBScript Find Multiple Words with One Search
From Regex Regular Expression Encyclopedia
You can use this recipe for finding one of a list of words in a line. This recipe assumes both words are whole words surrounded by whitespace and that the list is a short one containing the words moo and oink.
[edit] code
Dim fso,s,re,line,lineNbr Set fso = CreateObject("Scripting.FileSystemObject") Set s = fso.OpenTextFile(WScript.Arguments.Item(0), 1, True) Set re = New RegExp re.Pattern = "\s+(moo|oink)\s+" lineNbr = 0 Do While Not s.AtEndOfStream line = s.ReadLine() lineNbr = lineNbr + 1 If re.Test(line) Then WScript.Echo "Found match: '" & line & "' at line " & lineNbr End If Loop s.Close
[edit] How It Works
A special character class, \b, allows you to easily search for whole words. This is an advantage because without doing a whole bunch of extra work you can make sure that a search for some- thing, for example, doesn’t yield unexpected matches such as somethings. You can break the regular expression shown here into the following:
| Regular Expression | Description |
|---|---|
| \s | whitespace . . . |
| + | found one or more times . . . |
| (...) | followed by something . . . |
| \s | followed by whitespace . . . |
| + | that occurs one or more times. |
| The something here is another expression, moo|oink. This expression is as follows: | |
| m | an m, followed by . . . |
| o | an o, then . . . |
| o | an o . . . |
| or... | |
| o | an o, followed by . . . |
| i | an i, then . . . |
| n | an n, followed by... |
| k | a k. |
[edit] Variations
A useful variation of this recipe is to replace the \s+ combination, which matches specifically whitespace, with the word boundary character class \b.
