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. How can I do it?
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
So... Ignore the verbose code in my other answer. 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.