Search code examples
regexlinux

The method of using Regular Expression to express Date and Time: YYYY-MM-DD HH:MM:SS.XXX


How to use Regular Expression to express Date and Time format like this: YYYY-MM-DD HH:MM:SS.XXX, use the standard Regular Expression. For example, "2018-08-01 23:58:32.425","2018-08-01 23:58:29.250","2016-11-01 19:10:34.911", I list some time & date strings.


Solution

  • You can use the following regex:

    ^\d\d\d\d-(0?[1-9]|1[0-2])-(0?[1-9]|[12][0-9]|3[01]) (00|[0-9]|1[0-9]|2[0-3]):([0-9]|[0-5][0-9]):([0-9]|[0-5][0-9])\.(\d{3})$
    

    It will match the format:

    YYYY-MM-DD HH:MM:SS.fff
    

    Test the regex here.