Anirban Sen Chowdhary
Anirban Sen Chowdhary

Reputation: 8311

Need a regex expression to validate timestamp with utc

I have a date format like :- 2014-08-17T14:07:30+0521 or 2015-05-21T14:07:30+0521

Now I want to have a regex expression that can validate this date format..

I tried with the following format as given here How to check an UTC formatted date with a regular expression? :-

\d{4}-[0-1]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-6]\d:\d{3}\+\d\d:\d\d

but couldn't succeed .. Since I am very new to this regex expression, I am finding it difficult to construct the expression that could validate successfully the above timestamp format with utc

Please note that, I want regex expression, other solution option will not work for me

Thanks

Upvotes: 1

Views: 14477

Answers (4)

Waqas Ahmed
Waqas Ahmed

Reputation: 1928

 ^((19|20)[0-9][0-9])[-](0[1-9]|1[012])[-](0[1-9]|[12][0-9]|3[01])[T]([01][1-9]|[2][0-3])[:]([0-5][0-9])[:]([0-5][0-9])([+|-]([01][0-9]|[2][0-3])[:]([0-5][0-9])){0,1}$

2018-11-11T11:01:59+11:11

https://www.regextester.com/100092

Upvotes: 0

Anirban Sen Chowdhary
Anirban Sen Chowdhary

Reputation: 8311

Yes, the final solution I got using :-

\d{4}-[0-1]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\+\d{4}

Upvotes: 0

karthik manchala
karthik manchala

Reputation: 13640

You can use the following (the one you are using has different matching at the end):

\d{4}-[0-1]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d\+\d{4}

Edit: In Java you can use the following:

\\d{4}-[0-1]\\d-[0-3]\\dT[0-2]\\d:[0-5]\\d:[0-5]\\d\\+\\d{4}

Upvotes: 3

Zoltanik
Zoltanik

Reputation: 189

Try this one:

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

Upvotes: 4

Related Questions