python - How to match user introduced hour in format HH:MM -


i'm trying match introduced user hour 24 hours hh:mm format, not h:mm neither hh:m.

here's mi code

def validatedate(date1, date2): try:     time_re = re.compile(r'^(1?[0-9]|2[0-3]):[0-5][0-9]$')     match(time_re, date1)     match(time_re, date2) except :     print "datos o formato incorrecto, deberia ser hh:mm \n" 

try this:

import re  def validatedate(date1, date2):     try:         time_re = re.compile(r'^(0[0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$')         a=re.match(time_re, date1)         b=re.match(time_re, date2)         if none or b none:             return false         else:             return true      except :         print "foo"         raise 

there several problems there. first, regexp wrong, allowing 9:33 instead of requiring 09:33. however, re.match not raise exception if not match. returns match group if there match, or none if there isn't.

your generic exception clause triggered because have not imported re correctly. try adding raise exception handler , see complaining compile or match not found.

this why should never use except: clause if not know doing, catch syntactical errors in code making more difficult debug.

my modified function returns true if both date1 , date2 ok , false if either of them wrong. hope helps.

hannu


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -