Regular Expressions: More Complex Parsing : Parse « Development Class « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Development Class » ParseScreenshots 
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();
        }
    }
}
           
       
Related examples in the same category
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.