How to write a regex for MM: DD: YYYY: HH: MM: SS

How do I write a regex for MM: DD: YYYY: HH: MM: SS?

0


source to share


2 answers


If you want to write the values ​​by month, year, ... you can use something like this, I suppose:

([0-9]{2}):([0-9]{2}):([0-9]{4}):([0-9]{2}):([0-9]{2}):([0-9]{2})

      




If supported by your regex engine, \d

can be used as an alias [0-9]

:

(\d{2}):(\d{2}):(\d{4}):(\d{2}):(\d{2}):(\d{2})

      

+2


source


It depends if you want to check all digits separated characters, :

or you also want to check if you receive complete inappropriate data like December 32 or an hour like 25:66 . To have such basic validation, you must use a slightly more complex regex than the one provided by @Pascal MARTIN, for example



[0-1][0-9]:[0-3][0-9]:[1-2][0-9]{3}:[0-2][0-9]:[0-5][0-9]:[0-5][0-9]

      

+1


source







All Articles