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.
string timestamp_string = "2013/04/08 17:30"; DateTime timestamp = Convert. ToDateTime(timestamp_string); string time =""; if (timestamp_string. Length > 10) { time = timestamp. ToString("hh:mm"); } else { time = "Time not registered"; } MessageBox.
A date and time format string is a string of text used to interpret data values containing date and time information. Each format string consists of a combination of formats from an available format type. Some examples of format types are day of week, month, hour, and second.
You can check if a string is a date using the Python strptime() function from the datetime module. strptime() takes a string and a date format, and tries to create a datetime object. If the string matches the given string format, then the datetime object is created. If not, a ValueError occurs.
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();
}
}
}
string s = " Subject: Current account balances for 21st December 2017 and 2nd January 2019 mnmnm ";//here is u r sample string
s = s.ToLower();
string newStrstr = Regex.Replace(s, " {2,}", " ");//remove more than whitespace
string newst = Regex.Replace(newStrstr, @"([\s+][-/./_///://|/$/\s+]|[-/./_///://|/$/\s+][\s+])", "/");// remove unwanted whitespace eg 21 -dec- 2017 to 21-07-2017
newStrstr = newst.Trim();
Regex rx = new Regex(@"(st|nd|th|rd)");//21st-01-2017 to 21-01-2017
string sp = rx.Replace(newStrstr, "");
rx = new Regex(@"(([0-2][0-9]|[3][0-1]|[0-9])[-/./_///://|/$/\s+]([0][0-9]|[0-9]|[1][0-2]|jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec|january|february|march|april|may|june|july|augu|september|october|november|december)[-/./_///:/|/$/\s+][0-9]{2,4})");//a pattern for regex to check date format. For August we check Augu since we replaced the st earlier
MatchCollection mc = rx.Matches(sp);//look for strings that satisfy the above pattern regex
List<DateTime> dates=new List<DateTime>(); //Create a list to store the detected dates
foreach(Match m in mc)
{
string s2=Regex.Replace(m.ToString(), "augu", "august");
dates.Add(DateTime.Parse(s2);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With