-3

Need a regex with the following format: YYYY MMM DD HH:MM:SS

For example: 1995 FEB 10 12:12:20

Please advise.

Thanks!

  • if you're using javascript or python you would not use regex but utilise dedicated libraries where you pass in a pattern string. – Andrew Allen Jun 10 '19 at 15:52
  • 1
    Possible duplicate of [Learning Regular Expressions](https://stackoverflow.com/questions/4736/learning-regular-expressions) – jonrsharpe Jun 10 '19 at 15:53
  • So in python ```import datetime; datetime.datetime.strptime('1995 FEB 10 12:12:20', '%Y %b %d %H:%M:%S'); > datetime.datetime(1995, 2, 10, 12, 12, 20) ``` – Andrew Allen Jun 10 '19 at 15:56

1 Answers1

-1

This regex will catch that format:

\d{4} \w{3} \d{2} \d{2}:\d{2}:\d{2}

\d looks for digits.
\w looks for characters.
{#} looks for a specific number of occurrences of the previous character.

However, this will not validate the months, or the range of digits. This might be the closest that you can get, by using number ranges:

\d{4} (?:JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC) [1-3][0-9] [0-1][0-9]:[0-5][0-9]:[0-5][0-9]

Keep in mind that this can find dates like Feb 35. If you need to validate an address, you cannot (easily) do that with regex; you're much better off using functionality built into a programming language/library.

RToyo
  • 2,792
  • 1
  • 12
  • 21