使用正则表达式或其他一些解析从文件中读取值 [英] Reading values from a file using regex or some other parsing

查看:128
本文介绍了使用正则表达式或其他一些解析从文件中读取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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] +?雪佛兰尝试在同一行匹配雪佛兰
  • + <?/ code>匹配文本块...
  • (\ 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)

这篇关于使用正则表达式或其他一些解析从文件中读取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆