
Solution to Event 9 in the 2008 Winter Scripting Games Sudden Death Challenge.
If you’re still wondering what the Sudden Death Challenge was all about, well, this event serves as a pretty good example. We wanted to give people a few more things to keep them occupied during the Games. Furthermore, while we wanted to make these extra events a little challenging we didn’t want to make them overly-hard; that’s because competitors had only a limited time (less than 36 hours) to complete each of the 10 Sudden Death events. Our goal, therefore, was to come up with events that were relatively easy to solve once you figured out how to go about solving them. We just didn’t want to make it too terribly obvious how you should go about solving them.
Like we said, Event 9 is a good example of this. Once you figured out how to solve the problem (hint: use regular expressions) the solution should have taken no more than a few minutes to write and debug. But what if you didn’t figure out that regular expressions were the key to solving the problem? Well, that didn’t mean that you couldn’t solve the problem. However, it probably took you more than a few minutes, and more than a minimal amount of effort.
That was the idea anyway: events that were both easy and hard. Just like sweet-and-sour pork.
Well, sort of.
So was it really all that easy to solve Event 9? Well, see for yourself:
Const ForReading = 1
Set objRegEx = CreateObject("VBScript.RegExp")
Set objFSO = CreateObject("Scripting.FileSystemObject")
objRegEx.Global = True
objRegEx.Pattern = "[^A-Za-z0-9 ]"
Set objFile = objFSO.OpenTextFile("C:\Scripts\Symbols.txt", ForReading)
strSearchString = objFile.ReadAll
objFile.Close
strNewString = objRegEx.Replace(strSearchString, "")
Wscript.Echo strNewString
Note. This particular script was written using VBScript. However, you could have just as easily solved the problem using Windows PowerShell or Perl. |
As it turns out, and as you’re about to see, correcting this string was remarkably easy. To begin with, we define a constant named ForReading and set the value to 1; we’ll need that constant when we open our text file and read from it. After defining the constant we then create two objects: VBScript.RegExp, the object that enables us to use regular expressions in our script, and Scripting.FileSystemObject, the object that enables us to read from the text file.
Once we have our two objects in hand we need to define two property values for the regular expressions object. We first set the value of the Global property to True; that tells the script that we want to search for all instances of our target text, not just the first instance of that text. We then set the value of the Pattern property using this line of code:
objRegEx.Pattern = "[^A-Za-z0-9 ]"
As you probably know, the Pattern property represents our target text; it tells the script what it is we’re looking for. In this case, we’re looking for anything that does not belong to a specified set of characters; the ^ symbol means, “Is not part of the specified set of characters.” And what is the specified set of characters? It’s any character that isn’t:
| • | An uppercase letter. That’s what the range A-Z represents. |
| • | A lowercase letter. That’s what the range a-z represents. |
| • | A number. That’s what the range 0-9 represents. |
| • | A blank space. That’s what the, well, blank space at the end represents. |
In other words, we’re looking for anything that isn’t a letter, a number, or a blank space. That’s how we’re going to track down all the extraneous characters that were added to our string value.
After defining out pattern we open the text file C:\Scripts\Symbols.txt for reading, then use the ReadAll method to read the complete contents of the file, storing that information in the variable strSearchString. That makes strSearchString equal to this:
T#$h!e ()200$~``8 W,\i|n<:;>t.?e&^%-r S#=_cri??{}p()-t@@@i|[[ng G!@!am=_+e^%s/.,
Once we’ve read the file we use the Close method to close Symbols.txt; at this point, we have no more use for the file. That brings us to this line of code:
strNewString = objRegEx.Replace(strSearchString, "")
What we’re doing here is using the regular expression object’s Replace method to ferret out each instance of the target text and replace those instances with nothing. The net result? All the instances of the target text – all those extraneous symbols – will be removed from the string.
And how do we know if that really worked? Well, probably the easiest way to verify that is to simply echo back the value of strNewString and see what it looks like:
The 2008 Winter Scripting Games
And there you have it.