Csharp/C Sharp/Development Class/Parse — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 11:43, 26 мая 2010
Regular Expressions: More Complex Parsing
/*
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(?<date>[^\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 :
@"(?<time>(\d|\:)+)\s" +
// match digit or .
@"(?<ip>(\d|\.)+)\s" +
// match any non-white
@"(?<method>\S+)\s" +
// match any non-white
@"(?<uri>\S+)\s" +
// match any non-white
@"(?<status>\d+)");
// read through the lines, add an
// IISLogRow for each line
while ((line = stream.ReadLine()) != null)
{
//Console.WriteLine(line);
match = regexLine.Match(line);
if (match.Length != 0)
{
Console.WriteLine("date: {0} {1}", date,
match.Groups["time"]);
Console.WriteLine("IP Address: {0}",
match.Groups["ip"]);
Console.WriteLine("Method: {0}",
match.Groups["method"]);
Console.WriteLine("Status: {0}",
match.Groups["status"]);
Console.WriteLine("URI: {0}\n",
match.Groups["uri"]);
}
}
f.Close();
}
}
}