The code below fails to extract the date text and convert it – that’s what I’ve tried so far:
var inputText = "some words 2012-01-06 13-32-40 some words";
string pattern = "d{4}-d{2}-d{2} d{4}-d{2}-d{2}";
var regex = new Regex(pattern);
Match match = regex.Match(inputText);
// it should find the "2012-01-06 13-32-40" text
DateTime result;
if (match.Success)
{
result = DateTime.Parse(match.Value);
}
How to write the Regex pattern so that the date string part is extracted and how to convert this string to DateTime?
I tried:
IFormatProvider theCultureInfo = new System.Globalization.CultureInfo("en-GB", true);
result = DateTime.ParseExact(match.Value, "yyyy-MM-dd hh-mm-ss", theCultureInfo);
Thanks,
The Regex pattern would be:
Also you would have to parse the date string like this:
If the time was formatted like “13:32:40” you could just parse it like you have done it in your example.