The check_time function checks for the time format of a 12-hour clock, as follows: the hour is between 1 and 12, with no leading zero, followed by a colon, then minutes between 00 and 59, then an optional space, and then AM or PM, in upper or lower case. Fill in the regular expression to do that. How many of the concepts that you just learned can you use here

Respuesta :

Answer:

Following are the correct code to this question:

import re#import package for regular expression

def check_time(text):#defining a method check_time that accepts string value  

   p = r'(1[012]|[1-9]):[0-5][0-9][ ]{0,1}?(am|pm|AM|PM)'#defining string variable p that stores values

   val = re.search(p, text)#defining val variable that check serachs p and text variable values

   return val!= None#use return keyword to return val value

print(check_time("12:45pm"))#defining print method that calls method by input value  

print(check_time("9:59 AM")) #defining print method that calls method by input value

print(check_time("6:60 am")) #defining print method that calls method by input value

print(check_time("five o'clock"))#defining print method that calls method by input value

Output:

True

True

False

False

Step-by-step explanation:

In the above-given program, some data is missing that is code file so, the correct code can be defined as follows:

In the above-given method, that is "check time" it uses 12-hour time format validation, that is tested by coding the regex and  all the value validates in the "val" variables, that can be defined as  follows:  

  • In the first step, its values should be in  1,2,3, ... 10,11,12  
  • In the second step, it values in Between hour and minutes, and there will be a colon.  
  • In the third step, the minutes variable should take the double-digit, that will be like  00,01 .... 59.  
  • In the last step, one space becomes permitted after an hour: a minute or no space for am or pm value.

Q&A Education