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

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. XML
25. XML LINQ
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
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
w_w__w___._j___av_a__2s_._com | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.