发现如果字符串包含一个日期和时间 [英] Finding if a string contains a date and time

查看:157
本文介绍了发现如果字符串包含一个日期和时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个项目,我在这可以有两种不同格式的文件正在读,一个包括日期和时间,其他没有。

I am working on a project where I am reading in a file which could come in two different formats, one includes a date and time and the other doesn't.

当我在第一行我需要检查该串是否包含日期和时间,或不和读取文件并根据检查读入读出的文件某种方式。

When I read in the first line I need to check whether the string contains the date and time or not and read the file and based on the check read the file in a certain way.

我猜这将是某种形式的正则表达式,但不知道从哪里开始,并不能找到任何有关。

I'm guessing this would be some kind of regular expression but have no idea where to start and can't find anything relevant.

感谢您的帮助,您可以提供。

Thanks for any help you can provide.

更新
我不认为我已经为我所要求很明确。当我读到行的日志文件行的行可能会在为:

UPDATE I don't think I've been very clear as to what I am asking. When I read the log file line by line the line may come in as:

Col1   Col2  Col3  Col4  Col5 

有时,该行可能会在为

Col1  17-02-2013 02:05:00 Col2  Col3  Col4  Col5

当我读我需要做一个检查是否有包含在字符串中的日期和时间字符串的行。

When I read the line I need to do a check whether there is a date and time string contained within the string.

推荐答案

如果日期的格式已经被定义,你可以使用正则表达式来解决它。

If the format of the date has been defined, you can use Regex to solve it.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace RegTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string testDate = "3214312402-17-2013143214214";
            Regex rgx = new Regex(@"\d{2}-\d{2}-\d{4}");
            Match mat = rgx.Match(testDate);
            Console.WriteLine(mat.ToString());
            Console.ReadLine();
        }
    }
}

这篇关于发现如果字符串包含一个日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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