解析从日期时间多余字符 [英] Parsing extra characters from Datetime

查看:108
本文介绍了解析从日期时间多余字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有如下code表示从文件读取的日期。

 使用(VAR读卡器=新的StreamReader(@C:\ myfile.txt的))
{
    布尔发现= FALSE;
    而(!reader.EndOfStream)
  {
        。VAR线= reader.ReadLine()修剪();

        如果(发现和放大器;&安培; line.EndsWith(测试))
        {
            VAR fordDate = DateTime.Parse(line.Substring(0,19));
            Console.WriteLine(测试日期:{0},fordDate);
            打破;
        }
   }
 }
 

问题是,它提供了错误时的日期与它连接的其他文字。 例如

  \ r \ n2013-03-03 12时22分02秒
 

我试图去改变它,这样code可以删除\ r \ n或从任何其他文本和刚刚获得的日期部分。

解决方案

您应该使用常规EX pressions

如果您的日期是相同的格式一如既往,你可以很容易地写一个正前pression,将提取的日期从各行,并剥夺任何东西,每边。为了了解正前pression的目的应该是这样的:

  \ D {4}  -  \ d {2}  -  \ d {2} \ S \ D {2}:\ D {2}:\ D {2}
 

这经常EX pression过于简化,允许日期如 0000-00-00 99:99:99 这很可能是无效的。这取决于你的文件是否可以认为,可能apear为日期一些值,但不是。一个更复杂(但更有效)的前pression会(假定日期 YYYY-MM-DD ,而不是 YYYY-DD- MM ):

<$p$p><$c$c>[12]\d{3}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])\s(?:[01]\d|2[0-3]):(?:[0-5]\d):(?:[0-5]\d)

这人会允许日期从今年1000年至2999年以正确的月份数从01-12和天01-31和时间从00:00:00到23:59:59。

但是,为了使这个普通的前pression我会把它放在括号中,并给它一个名称,以便这些日期将成为一个名为捕获组(部分更有益日期)在code,您可以使用它的名称,而不是索引访问。

 正则表达式RX = "(?<date>[12]\d{3}-(?:0\d|1[0-2])-(?:0[1-9]|[12]\d|3[01])\s(?:[01]\d|2[0-3]):(?:[0-5]\d):(?:[0-5]\d)).*Test$";
如果(rx.Text(线))
{
    匹配M = rx.Match(线);
    //没有必要使用的TryParse为正则表达式确保正确的格式
    fordDate = DateTime.Parse(m.Groups [日]);
}
 

所以,而不是手动检查与的行结束测试我还包含在常规EX pression同样的要求。

Hi I have following code that reads the date from a file.

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

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

Problem is that it gives error when date has some other text connected with it. For example

\r\n2013-03-03 12:22:02 

I am trying to change it so that code can remove "\r\n" or any other text from it and just get the date part.

解决方案

You should use regular expressions

If your dates are always of the same format, you can easily write a regular expression that will extract dates from individual lines and strip anything else on each side. For the purpose of understanding regular expression should look like this:

\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}

This regular expression is too simplified and allows dates like 0000-00-00 99:99:99 which is likely invalid. It depends whether your file can hold some values that may apear as dates, but are not. A more complex (but more valid) expression would be (assuming that date is YYYY-MM-DD and not YYYY-DD-MM):

[12]\d{3}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])\s(?:[01]\d|2[0-3]):(?:[0-5]\d):(?:[0-5]\d)

This one will allow dates from year 1000 to 2999 with correct month numbers from 01-12 and days from 01-31 and hours from 00:00:00 to 23:59:59.

But to make this regular expression more useful I'll put it in parentheses and give it a name so these dates will become part of a named capture group (date) in your code that you can access using its name rather than index.

Regex rx = "(?<date>[12]\d{3}-(?:0\d|1[0-2])-(?:0[1-9]|[12]\d|3[01])\s(?:[01]\d|2[0-3]):(?:[0-5]\d):(?:[0-5]\d)).*Test$";
if (rx.Text(line))
{
    Match m = rx.Match(line);
    // no need to use TryParse as regex assures correct formatting
    fordDate = DateTime.Parse(m.Groups["date"]);
}

So instead of checking manually that line ends with Test I've also included the same requirement in the regular expression.

这篇关于解析从日期时间多余字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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