Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Regex question
Message
From
18/04/2010 08:35:23
 
General information
Forum:
ASP.NET
Category:
Other
Title:
Environment versions
Environment:
C# 2.0
OS:
Windows XP SP2
Network:
Windows 2000 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
01460501
Message ID:
01460800
Views:
61
>Hi All, I need to parse some log files of an old system, can anyone give me a heads up on the Regex I need ?
>The log format is shown below
>
>09/06/2009 13:09:54 - PJK accessed MCN: 4403010057 - Mr Peter Kane
>
>
>I need to parse the 4403010057 part
>
>I know I could use Indexof in the string class but I think this requirement could grow horns so I want to start cutting my teeth on Regexes - eventually I will bone up up on them but I don't have the time at present - as usual any help much appreciated

Pete,

Is this helpful at all ?

When parsing the line: DateTime, Message, Number and Who are captured into named groups
The named groups are used to construct a class LogEntry

Passing a StreamReader to LogParser.Parse() parses each line and makes a LogEntry object per line

The method LogParser.Parse() returns a list of LogEntry

Output:
«09/06/2009 13:09:54» «PJK accessed MCN» «4403010057» «Mr Peter Kane»
«10/06/2009 14:09:54» «ABC accessed QRS» «5403010057» «Mr Alfred Cucumber»
Sample program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

//using GregoryAdam.Base.Debugging;
//using GregoryAdam.Base.Text.RegEx;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using System.Diagnostics;


namespace BaseTest
{
	//______________________________________________________________________
	class Program
	{
		
		static string LogContents = 
@"09/06/2009 13:09:54 - PJK accessed MCN: 4403010057 - Mr Peter Kane
10/06/2009 14:09:54 - ABC accessed QRS: 5403010057 - Mr Alfred Cucumber";
		public static void Main()
		{
			using (StreamReader  rdr = new StreamReader(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(LogContents))) )
			{
				List<LogEntry> list = LogParser.Parse(rdr);

				foreach (LogEntry entry in list)
				{
					Console.WriteLine("«{0}» «{1}» «{2}» «{3}» ",
						entry.When,
						entry.Message,
						entry.Number,
						entry.Who
						);
				}
			}

			
			Console.ReadLine();
		}

	}
	//______________________________________________________________________
	public static class LogParser
	{

		static string LinePattern =
			// 09/06/2009 13:09:54 - PJK accessed MCN: 4403010057 - Mr Peter Kane

		@"^(?<DateTime>\d{2}\/\d{2}\/\d{4}\s+\d{2}:\d{2}:\d{2})\s+-\s+(?<Message>[^:]+):\s+(?<Number>\d+)\s+-\s+(?<Who>.+)$";
		//______________________________________________________________________
		public static List<LogEntry> Parse(StreamReader stream)
		{
			var list = new List<LogEntry>();

			while (!stream.EndOfStream)
			{

				string line = stream.ReadLine();
				Match match = Regex.Match(line, LinePattern);

				if (match.Success)
				{

					list.Add(new LogEntry(
										Convert.ToDateTime(match.Groups["DateTime"].Value),
										match.Groups["Message"].Value,
										match.Groups["Number"].Value,
										match.Groups["Who"].Value
										)
							);
				}
				else
				{
					Debug.Assert(false, "cannot parse", line);
				}

			}
			return list;
		}
	}
		//______________________________________________________________________
	public class LogEntry
	{
		public readonly DateTime When;
		public readonly string Message;
		public readonly string Number;
		public readonly string Who;
		//______________________________________________________________________
		public LogEntry
			(	DateTime when,
				string message,
				string number,
				string who
			)

		{
			When = when;
			Message = message;
			Number = number;
			Who = who;
		}
		//______________________________________________________________________
	}
}
Gregory
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform