Regex for time in hh:mm am/pm format

后端 未结 5 1941
暖寄归人
暖寄归人 2020-11-29 10:32

I need to make strict validation of input for a school project, time in format HH:MM am/pm. So far i\'ve got this RegEx expression:

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


        
相关标签:
5条回答
  • 2020-11-29 10:44
      ([0-9]|0[0-9]|1[0-9]|2[0-3]):([0-5][0-9])\s*([AaPp][Mm])
    

    For the following:

    10:00AM

    10:00PM

    10:00am

    10:00pm

    10:00 pm

    10:00 am

    10:30PM

    23:46am

    0 讨论(0)
  • 2020-11-29 10:51

    I used this regex

    ([0-9]{2})\\/([0-9]{2})\\/([0-9]{4}) ([0-9]{2}):([0-9]{2}):([0-9]{2}) (am|pm)
    

    You can try this. Hope this helps!

    0 讨论(0)
  • 2020-11-29 10:55
      String regex = "([01]?[0-9]|2[0-3]):[0-5][0-9]";
      
    

    This will work for the 24 hours validation

    0 讨论(0)
  • 2020-11-29 10:59

    This regex has 3 parts, separated by the OR operator.
    Each part allows for a type of HH
    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)))/
    
    0 讨论(0)
  • 2020-11-29 11:05

    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
    
    0 讨论(0)
提交回复
热议问题