Finding if a string contains a date and time
c#
Solution
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();
}
}
}
Problem
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 ``` Sometimes the line may come in as ``` 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.