9
$\begingroup$

I have a list of strings,

{"BD4675365", "Corp", "Managers", "Syndicate", "2021-03-08", "8434926"}

and I need to extract the date. It may be in any position.

Can I match a string to see if it is in "date" format, in this case ****-**-**, then extract it from the list?

$\endgroup$
9
  • 3
    $\begingroup$ Have you seen FromDateString[]? (And Check[]?) $\endgroup$
    – Michael E2
    May 24, 2023 at 15:48
  • $\begingroup$ Does FromDateString give a different output if the input is not a date? I suppose this is what Check does. $\endgroup$
    – apg
    May 24, 2023 at 15:50
  • $\begingroup$ So Check[FromDateString["2021-03-08"], 0], then remove the zeros. $\endgroup$
    – apg
    May 24, 2023 at 15:54
  • $\begingroup$ I'm not sure exactly how you want to use it: Do you want to extract the strings that are dates, or do you want a Mathematica date object instead of a string. You could post-process the extracted strings, but that's an unnecessary step. (You could do what you suggest in your last comment, for instance. There may be better ways, though.) $\endgroup$
    – Michael E2
    May 24, 2023 at 15:57
  • 1
    $\begingroup$ The format "ISODate" seems also to work. I'm not sure how flexible/rigid the formatting of the dates in your data is. $\endgroup$
    – Michael E2
    May 24, 2023 at 16:08

4 Answers 4

15
$\begingroup$

The pattern "DatePattern[{"Year", "Month", "Day"}]" will match a date. You may use it e.g. inside "Select" like:

list = {"BD4675365", "Corp", "Managers", "Syndicate", "2021-03-08", 
   "8434926"};
Select[list, StringMatchQ[#, DatePattern[{"Year", "Month", "Day"}]] &]

{"2021-03-08"}
$\endgroup$
1
  • 1
    $\begingroup$ Holy cow I never even knew DatePattern was a thing, and it's been a thing since 2007! $\endgroup$
    – Pillsy
    May 24, 2023 at 19:22
9
$\begingroup$

Extract the date strings:

dateStringQ[s_String] := 
  Quiet[Check[FromDateString[s, {"Year", "-", "Month", "-", "Day"}]; 
    True, False, FromDateString::str], FromDateString::str];

Cases[{"BD4675365", "Corp", "Managers", "Syndicate", "2021-03-08", 
  "8434926"}, _?dateStringQ]

(*  {"2021-03-08"}  *)

Extract the date objects:

DeleteCases[
  FromDateString[
   {"BD4675365", "Corp", "Managers", "Syndicate", 
    "2021-03-08", "8434926"},
   {"Year", "-", "Month", "-", "Day"}],
  _?FailureQ] // Quiet

(*  {DateObject[{2021, 3, 8}, "Day"]}  *)
$\endgroup$
0
6
$\begingroup$
(Interpreter["Date"][#] & /@ list ) /. _Failure :> Nothing

Check[SemanticImportString[#, "Date"], Nothing] & /@ list // 
  Map[First] // Normal

enter image description here

$\endgroup$
1
$\begingroup$

It's always an overkill to use regex.

{"BD4675365", "Corp", "Managers", "Syndicate", "2021-03-08", "8434926"} //
Select[StringMatchQ[RegularExpression["^\d{4}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])$"]]]

regex from https://stackoverflow.com/a/22061879/13040423

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.