Regular Expression Vowel Counter
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
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,
Regex vowels = new Regex("[aeiou]", RegexOptions.IgnoreCase | RegexOptions.Compiled);
MatchCollection mc;
FileStream fs = null;
StreamReader reader = null;;
String current_line;
try {
fs = new FileStream("FileName.txt", FileMode.Open);
reader = new StreamReader(fs);
}
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);
Console.Out.WriteLine("The search term \"" + current_line + "\" has " + mc.Count + " vowels in it");
}
reader.Close();
fs.Close();
}
}
}
Comments
Post a Comment