0

Want to have a correct pattern for below

18-11-2023 16:03:22

DD-MM-YYYY HH24:MM:SS

I am using below pattern and it works fine but if I pass date or month as single digit it doesn't work. How can I fix this?

"pattern": "^([1-9]|([012][0-9])|(3[01]))-([0]{0,1}[1-9]|1[012])-\\d\\d\\d\\d [012]{0,1}[0-9]:[0-6][0-9]:[0-6][0-9]$"

Maddy
  • 674
  • 1
  • 7
  • 26

1 Answers1

1

This blocks single digit days and months - see regex101.

I only removed the double \\ to make it regex101 compatible.

^(([012][0-9])|(3[01]))-([0][1-9]|1[012])-\\d\\d\\d\\d [012]{0,1}[0-9]:[0-6][0-9]:[0-6][0-9]$

In json-schema each \ has to be escaped by another \. See here

John Williams
  • 4,252
  • 2
  • 9
  • 18
  • If I remove that escaped character it gives an error in parsing the JSON. Added a pic to show that. – Maddy Apr 27 '23 at 00:29
  • You need to reinstate the escape \ but the regex **works** with single digit day and month. Updated answer to clarify. – John Williams Apr 27 '23 at 09:33
  • John, I want it to fail if single digit day or month was provided. It is currently passing with that so wanted to know how to fix this. – Maddy Apr 27 '23 at 10:07
  • 1
    @Maddy - I have fixed the regex to fail on single digit day and month. – John Williams Apr 28 '23 at 06:56