J. Davidson
J. Davidson

Reputation: 3307

Reading values from a file using regex or some other parsing

I have a file that logs values with a timestamp. I have to read a certain value after a specific time.

For example

File has

2013-03-03 19:08:22    car   2001 Ford
2013-03-03 19:08:27    Truck 2012 Chevy
2013-03-03 19:08:44    car 2008   Honda
2013-03-03 19:08:55    car 2011   Ford
2013-03-03 19:09:21    car 2005   Nissan
2013-03-03 19:08:29    car 2003   Cadillac
2013-03-03 19:08:32    car 2009   Jeep
2013-03-03 19:08:52    car 2007   Suburban

I want to read the time of the First instance of Chevy than add 40 seconds to it then read whichever Ford appears after that time.

So based upon the above log
First Chevy is at   19:08:27  
Add 40 seconds      19:09:07
Now Read first Ford that shows up after 19:09:07  in this case that would be one at 19:08:55

I am new to regex don't know for sure how to write it. Thanks

Upvotes: 2

Views: 380

Answers (1)

Oscar Mederos
Oscar Mederos

Reputation: 29823

First of all, I really don't suggest you using Regex for that, but here is one you could use:

var data =  @"2013-03-03 19:08:22    car   2001 Ford
2013-03-03 19:08:27    Truck 2012 Chevy
2013-03-03 19:08:44    car 2008   Honda
2013-03-03 19:08:55    car 2011   Ford
2013-03-03 19:09:21    car 2005   Nissan
2013-03-03 19:08:29    car 2003   Cadillac
2013-03-03 19:08:32    car 2009   Jeep
2013-03-03 19:08:52    car 2007   Suburban";

string regex =
    @"(\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}:\d{2})[^\n\r]+?Chevy.+?(\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}:\d{2})[^\n\r]+?Ford";
var m = Regex.Match(data, regex, RegexOptions.Singleline);

Console.WriteLine("Chevy date: {0}", DateTime.Parse(m.Groups[1].Value));
Console.WriteLine("Ford date: {0}", DateTime.Parse(m.Groups[2].Value));

The above code will print:

Chevy date: 2013-03-03 19:08:27
Ford date: 2013-03-03 19:08:55

All you have to do is add 40 seconds. You can do it using the AddSeconds(..) method in DateTime.

Edit: The above regex explained:

  • (\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}:\d{2}) Match some DateTime
  • [^\n\r]+?Chevy Try to match "Chevy" in the same line
  • .+? Match a chunk of text...
  • (\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}:\d{2}) Until it matches some DateTime again...
  • [^\n\r]+?Ford ...which has "Ford" in the same line

Edit: Here is another approach that doesn't require using regular expressions:

using (var reader = new StreamReader(@"C:\file-here.txt")) {
    bool chevyFound = false;
    while (!reader.EndOfStream) {
        var line = reader.ReadLine().Trim();

        if (chevyFound && line.EndsWith("Ford")) {
            var fordDate = DateTime.Parse(line.Substring(0, 19));
            Console.WriteLine("Ford Date: {0}", fordDate);
            break;
        }

        if (line.EndsWith("Chevy")) {
            var chevyDate = DateTime.Parse(line.Substring(0, 19));
            Console.WriteLine("Chevy Date: {0}", chevyDate);
            chevyFound = true;
        }
    }
}

Perhaps when someone told you you should use regular expressions, he/she meant when parsing the DateTimes (instead of doing Substring(0,19)). In that case, you can replace it by:

var chevyDate = DateTime.Parse(Regex.Match(line, "^\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}:\d{2}").Value)

Upvotes: 1

Related Questions