views:

22

answers:

2

Hello,

I have logs stored in a txt file in the following format

======8/4/2010 10:20:45 AM=========================================

Processing Donation

======8/4/2010 10:21:42A M=========================================

Sending information to server

======8/4/2010 10:21:43 AM=========================================

I need to parse these lines into a list where the information betweeen "====" lines is counted as one record to display on web page using paging in ASP.NET MVC.

Example the first record entry would be

======8/4/2010 10:20:45 AM=================================================

Processing Donation

I had no luck so far. I was hoping if some one could help me with it.

Thanks in advance.

A: 

Whilst reading in the file could you do a check to see if the line ends with =====

var sBuilder = new StringBuilder()
bool lineEnd = false;
var items = new List<string>();
string currentLine = String.Empty
using(var file = new StringReader("log.txt"))
{
  while( (currentLine = file.ReadLine()) != null)
  {
    if(currentLine.EndsWith("===="))
    {
        items.Add(sBuilder.ToString());
        sBuilder.Clear();
    }
    else
        sBuilder.Append(currentLine);
  }
}

It's a bit verbose but might give you some ideas

Dylan
A: 

so................... ignore my verbose code above. Instead use this two line wonder

string texty = "=====........";//file data
var matches = Regex.Matches(texty, @"={6}(?<Date>.+)={41}\s*(?<Message>.+)");

var results = matches.Cast<Match>().Select(m => new {Date = m.Groups["Date"], Message = m.Groups["Message"]});

I always forget about regular expressions

Dylan