Regex and File Stream Example


Regex and File Stream Example

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();
 }
  }
 }
Advertisements

About sharepointsriram

9+ Yrs of IT experience
This entry was posted in C# Programming. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s