/* A Programmer's Introduction to C# (Second Edition) by Eric Gunnerson Publisher: Apress L.P. ISBN: 1-893115-62-3 */ // 17 - Strings\Regular Expressions\More Complex Parsing // copyright 2000 Eric Gunnerson // file=logparse.cs // compile with: csc logparse.cs using System; using System.Net; using System.IO; using System.Text.RegularExpressions; using System.Collections; public class MoreComplexParsing { public static void Main(string[] args) { if (args.Length == 0) //we need a file to parse { Console.WriteLine("No log file specified."); } else ParseLogFile(args[0]); } public static void ParseLogFile(string filename) { if (!System.IO.File.Exists(filename)) { Console.WriteLine ("The file specified does not exist."); } else { FileStream f = new FileStream(filename, FileMode.Open); StreamReader stream = new StreamReader(f);
string line; line = stream.ReadLine(); // header line line = stream.ReadLine(); // version line line = stream.ReadLine(); // Date line
Regex regexDate= new Regex(@"\:\s(?[^\s]+)\s"); Match match = regexDate.Match(line); string date = ""; if (match.Length != 0) date = match.Groups["date"].ToString();
line = stream.ReadLine(); // Fields line
Regex regexLine = new Regex( // match digit or : @"(?