Given a date time format string, is there a standard way to find the first matching substring that matches that format?
for example, given...
d-MMM-yy H:mm:ss
and some text...
"blah 1 2 3 7-Jul-13 6:15:00 4 5 6 blah"
I'd expect it to return
"7-Jul-13 6:15:00"
Now I can find this string by doing parsing, but I'm wondering if there is any library support for doing this?
The literal "Z" is actually part of the ISO 8601 datetime standard for UTC times. When "Z" (Zulu) is tacked on the end of a time, it indicates that that time is UTC, so really the literal Z is part of the time.
A date and time format string defines the text representation of a DateTime or DateTimeOffset value that results from a formatting operation. It can also define the representation of a date and time value that is required in a parsing operation in order to successfully convert the string to a date and time.
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.
This may not be the most efficient but it seemed like an interesting question so I thought I'd try this method.
It takes your DateTime format string and makes a Regex string pattern out of it by replacing any letters with .
and whitespace with \\s
. It then creates a Regex object out of that pattern and tries to find the first match in the input sentence.
That match, if it exists, is then passed into a DateTime.TryParseExact
call. I'm sure improvements can be made but this might help give a general idea on a technique that doesn't require hardcoding a Regex or the format of the input sentence.
string inputSentence = "blah 1 2 3 7-Jul-13 6:15:00 4 5 6 blah";
string dtformat = "d-MMM-yy H:mm:ss";
//convert dtformat into regex pattern
StringBuilder sb = new StringBuilder();
foreach (char c in dtformat)
{
if (Char.IsLetter(c))
{
if (char.ToUpperInvariant(c) == 'D' || char.ToUpperInvariant(c) == 'H' || char.ToUpperInvariant(c) == 'S')
sb.Append(".{1,2}");
else
sb.Append(".");
}
else if(Char.IsWhiteSpace(c))
sb.Append("\\s");
else
sb.Append(c);
}
string dtPattern = sb.ToString();
Regex dtrx = new Regex(dtPattern);
//get the match using the regex pattern
var dtMatch = dtrx.Match(inputSentence);
if(dtMatch != null)
{
string firstString = dtMatch.Value.Trim();
//try and parse the datetime from the string
DateTime firstMatch;
if (DateTime.TryParseExact(dstr, dtformat, null, DateTimeStyles.None, out firstMatch))
{
Console.WriteLine("Parsed");
}
else
{
Console.WriteLine("Could not parse");
}
}
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