Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to find the first string that matches a DateTime format string?

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?

like image 473
Keith Nicholas Avatar asked Jul 08 '13 00:07

Keith Nicholas


People also ask

What is DateTime string Z?

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.

Is DateTime a string format?

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.

How do you check if a string is in a date in Python?

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.


1 Answers

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");
    }
}
like image 97
keyboardP Avatar answered Oct 25 '22 23:10

keyboardP