regex to validate dateformat from url and otherwise

Viewed 22

I need a regex, that validates this 2 patterns:

yyyy-MM-dd HH:mm:ss

and

yyyy-MM-dd%20HH:mm:ss

the second one is for Get parameters of a url.. thats why there is a %20 in there.

Edit:

I tired this one:

^\d{4}\/([0]\d|[1][0-2])\/([0-2]\d|[3][0-1])(\s([0]\d|[1][0-2])(:[0-5]\d){1,2})*\s*([aApP][mM]{0,2})?$

but it does not work

1 Answers
[0-9]{4}-[0-9]{2}-[0-9]{2}(?:%20| )[0-9]{2}:[0-9]{2}:[0-9]{2}

This'll work if you're in JavaScript.

You can also place the separate parts in capture groups and check them if you need to validate e.g. that month is less than 12

Related