Covers topics on the Microsoft Certification Exam for the .NET Framework (Exam 70-536, Microsoft .NET Framework - Application Development Foundation)

Saturday, December 6, 2008

Regular Expression Vowel Counter

In this example we are going to touch on two objectives of the 70-536 exam.

Implementing globalization, drawing, and text manipulation functionality in a .NET Framework application
  • Regex class
  • MatchCollection class

Implementing serialization and input/output functionality in a .NET Framework application
  • FileStream class
  • StreamReader class
To show these classes in use we are going to count the number of vowels in 25 popular search terms (popular according to Lycos) These strings are included in a link in the resources.

Regex and File Stream Example

using System;
using System.Text.RegularExpressions;
using System.IO;

namespace SimpleRegExExample
{
class Program
{
static void Main(string[] args)
{
// In this example we are going to hit two exam points,
// using a file stream and using a regular expression.
// This example only touches on the use of regular
// expressions... in fact there are whole books on the subject
// as it can be cosidered a language it self. Feel free
// to comment on specific regular expressions you want
// to see examples on.

//Match only the vowels...
Regex vowels = new Regex("[aeiou]", RegexOptions.IgnoreCase | RegexOptions.Compiled);
MatchCollection mc;
FileStream fs = null;
StreamReader reader = null; ;
String current_line;

// A filestream can throw a number of exceptions if the file
// you use to create it can't be found or accessed ( such as
// security reasons, a disc error, etc)

try
{
// For this example we are reading in a text file with 25 lines of text.
// It's hard to make up that much data, so I just took the top 26-50
// search terms on lycos and placed them in a text file. Some of the terms
// were kind of racy, like "Kendra Wilkinson Playboy Playmate"...
// If you stumbled here hoping to find pictures of said search term, this
// is the wrong place.
fs = new FileStream("lycos_top_search_terms_26_to_50.txt",FileMode.Open);

//Life is easier when we wrap the filestream ina stream reader,
// then we don't have to code the buffering for read line operations
reader = new StreamReader(fs);
}

// This is bad, you really should catch specific exceptions...
// but we skip it for this example.
catch (Exception e)
{
Console.Out.WriteLine(e.Message);
Console.Out.WriteLine(e.StackTrace);
if (reader != null)
{
reader.Close();
}
if (fs != null)
{
fs.Close();
}
return;
}

while (!reader.EndOfStream)
{
current_line = reader.ReadLine();
mc = vowels.Matches(current_line);

// The match collect now contains the results of running the regular
// against the line we read in from the file
Console.Out.WriteLine("The search term \"" + current_line + "\" has " + mc.Count + " vowels in it");
}

//Close the reader first
reader.Close();
//Now close the file
fs.Close();
}
}
}

Output
The search term "Flickr Picture" has 4 vowels in it
The search term "Brooke Burke Dancing star" has 8 vowels in it
The search term "Kendra Wilkinson Playboy Playmate" has 10 vowels in it
The search term "Julianne Hough Watching stars" has 9 vowels in it
The search term "Twilight Vampire movie out now" has 11 vowels in it
The search term "Lindsay Lohan Relatively quiet week" has 13 vowels in it
The search term "Holly Madison Playboy Playmate #2" has 9 vowels in it
The search term "Black Friday Look for your local ad" has 11 vowels in it
The search term "Harry Potter New movie in the works" has 10 vowels in it
The search term "Weight Watchers Getting ready for the new year" has 13 vowels in it
The search term "Carmen Electra Taking it slow" has 9 vowels in it
The search term "Jessica Alba New Mom" has 7 vowels in it
The search term "Limewire Download site" has 9 vowels in it
The search term "Fox News National new coverage" has 11 vowels in it
The search term "Lowes Do-it-yourself" has 7 vowels in it
The search term "Santa Claus Coming to town" has 8 vowels in it
The search term "Beyonce Knowles Is Sasha Fierce" has 11 vowels in it
The search term "Home Depot The Lumberyard" has 8 vowels in it
The search term "Paula Abdul Leaving American Idol?" has 14 vowels in it
The search term "US Postal Service Holiday Packages " has 12 vowels in it
The search term "Jennifer Lopez Keeping it quiet" has 12 vowels in it
The search term "Haley Joel Osment On Broadway" has 10 vowels in it
The search term "Lil Wayne New CD release" has 8 vowels in it
The search term "Angelina Jolie Mom again?" has 11 vowels in it
The search term "Plentyoffish.com Dating site" has 8 vowels in it


Additional Resources
Introductions to Regular Expressions (Microsoft)
FileStream Class (Microsoft)
StreamReader Class (Microsoft)
Regular Expression Designer (Rad Software, a must have for non trivial regular expression testing)
Test File used in this example

non code related...
Lycos top 50 search terms (Lycos)

No comments:

Support This Site

LinkShare  Referral  Prg