Regex to check UTC time format

I want to check the date format, which is similar to '2014-08-29T06: 44: 03Z' for this I am looking for reg.

I tried several combinations, but for me it did not work.

+6
source share
3 answers

Try this regex

\b[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z\b 

There:

\b is the word boundary to verify that the example "92014-08-29T06: 44: 03Z" is invalid

[0-9]{n} - number of coincidence with n digits

If the string should contain only date-time and other characters, use:

 ^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$ 
+7
source

The following regular expression accepts a UTC ISO-8601 date time string that provides:

Format

: YYYY-MM-DDTch: mm: ssZ
example: 2016-07-08T12: 30: 00Z

Where:
YYYY = 0000 to 9999
MM = 01 to 12
DD = 01 to 31
hh = 00 to 23
mm = 00 to 59
ss = 00 to 59

 \d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[1-2]\d|3[0-1])T(?:[0-1]\d|2[0-3]):[0-5]\d:[0-5]\dZ 

You can check it here: https://regex101.com/r/jE4cE4/1

+2
source

Answers to gum will definitely work, here are a few more examples.

A little more briefly:

 \d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z 

A bit more accuracy:

 \d{4}-[01]{1}\d{1}-[0-3]{1}\d{1}T[0-2]{1}\d{1}:[0-6]{1}\d{1}:[0-6]{1}\d{1}Z 

Of course, you can write more advanced expressions that are more efficient than this, using a back and back link to really make sure that you have a 100% verified match for valid combinations, etc.

0
source

Source: https://habr.com/ru/post/974546/


All Articles