使用正则表达式或其他一些解析从文件中读取值或其他、文件、正则表达式

2023-09-07 08:58:39 作者:嗱掵去搏

我有一个记录值与时间戳的文件。我之后的特定时间阅读一定的价值。

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

例如

文件

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

我想读雪佛兰初审的时间比加40秒,这两者福特看来时间之后不是阅读。

I want to read time of First instance of Chevy than add 40 seconds to it than 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 dont know for sure how to write it. Thanks

推荐答案

首先,我真的不建议你使用正则表达式的,但这里是一个你可以使用:

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));

以上code会打印:

The above code will print:

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

所有你需要做的就是加40秒。您可以使用 AddSeconds做(..)的方法的DateTime

编辑:上述正则表达式解释的:

The above regex explained:

(\ d {4} - \ d {2} - \ d {2} \ S + \ D {2}:\ D {2}:\ D {2})匹配某些日期时间 [^ \ñ\ R] +?雪佛兰尝试在同一行匹配雪佛兰 + 匹配文本块... (\ d {4} - \ d {2} - \ d {2} \ S + \ D {2}:\ D {2}:\ D {2}),直到它再次与一些日期时间... [^ \ñ\ R] +?福特 ...这在同一条线上有福特 (\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

修改:下面是不需要使用常规EX pressions另一种方法:

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;
        }
    }
}

也许,当有人告诉你,你应该使用常规EX pressions,他/她在解析时的DateTime是否意味着(而不是做子串(0,19)) 。在这种情况下,您可以通过替换它:

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)
 
精彩推荐
图片推荐