VBScript Find Variations on Words
From Regex Regular Expression Encyclopedia
You can use this recipe for finding variations on a word with one search. This particular recipe searches for the strings Jon Doe, John Doe, or Jonathan Doe.
[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 = "\b[bcm]at\b" 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
This expression works by finding the common and optional parts of a word and searching based on them. John, Jon, and Jonathan are all similar. They start with Jo and have an n in them. The rest is the h in John or the athan ending in Jonathan. For example:
| Regular Expression | Description |
|---|---|
| \b | a word boundary . . . |
| J | followed by . . . |
| o | then . . . |
| h | that is . . . |
| ? | optional, followed by . . . |
| n | followed by . . . |
| (...) | a group of characters . . . |
| ? | that may appear once but isn’t required, followed by . . . |
| <space> | a space, followed by . . . |
| D | then . . . |
| o | and finally . . . |
| e | an e, then . . . |
| \b | another word boundary. |
This group of characters is athan, which will let the expression match Jonathan. It may or may not appear as a whole part, so that’s why it’s grouped with parentheses and followed by ?.
[edit] Variations
One variation on this recipe is using instead an expression such as that found in recipe 1-3, like ((Jon)|(John)|(Jonathan)) Doe. Depending on the skills of your peers, this may be easy to use because it may be easier to read by someone else. Another variation on this is ((Jon(athan)?)|(John)) Doe. Writing an elegant and fast regular expression is nice, but these days processor cycles are often cheaper than labor. Make sure whatever path you choose will be the easiest to maintain by the people in your organization.
