Regular expression for time in hh format: mm am / pm

I need to do a strict input check for a school project, time in HH format: MM am / pm. So far I have this RegEx expression:

(([01]?[0-9]):([0-5][0-9]) ([AaPp][Mm]))

here's a working demo: http://regexr.com/3c9b5

The only problem is that it takes time from 13:00 to 19:59

What is the correct regular expression? RegEx has always been hard for me

+4
source share
2 answers

you can use:

\b((1[0-2]|0?[1-9]):([0-5][0-9]) ([AaPp][Mm]))
EDIT:         ^ changed this from 0-1 to not accept 00:00
+10
source

This regular expression consists of three parts, separated by the OR operator.
Each part is HH type.
They are 0 [1-9], [1-9] and 1 [0-2].

/(0[1-9]:[0-5][0-9]((\ ){0,1})((AM)|(PM)|(am)|(pm)))|([1-9]:[0-5][0-9]((\ ){0,1})((AM)|(PM)|(am)|(pm)))|(1[0-2]:[0-5][0-9]((\ ){0,1})((AM)|(PM)|(am)|(pm)))/
+1
source

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


All Articles