Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to match RFC3339 timestamp using Regex?

Tags:

regex

erlang

I want i check the format of timestamp which is described in RFC3339.

Here is the examples:

"1996-12-19T16%3A39%3A57-08%3A00"

"1996-12-19T15%3A39%3A27%2E20-08%3A00"

"2013-07-08T18%3A07%3A13Z"

"2013-07-08T18%3A07%3A13.49Z“

I have run one re pattern, but it only works for the first and second examples.

"\\d{4}-\\d{2}-\\d{2}T\\d{2}%3A\\d{2}%3A\\d{2}(%2E\\d+)?[+-]\\d{2}%3A\\d{2}"
like image 722
BlackMamba Avatar asked Jul 03 '14 02:07

BlackMamba


1 Answers

The below regex would match all the above four examples,

"^\\d{4}-\\d{2}-\\d{2}T\\d{2}%3A\\d{2}%3A\\d{2}(?:%2E\\d+)?[A-Z]?(?:[+.-](?:08%3A\\d{2}|\\d{2}[A-Z]))?$"

DEMO

You forget to mark [+-]\\d{2}%3A\\d{2} as optional and also forget to add regex to match Z and .49Z in third and fourth line.

like image 155
Avinash Raj Avatar answered Oct 07 '22 05:10

Avinash Raj