1

How do I check if a string coming into a function has the correct format? The correct format is "8:30:00" or "16:00:00"

Something like:

string_format = "%h%m%s"

if input_data == string_format:
    do this 
else:
    error 
3

2 Answers 2

4

You can do it without using regex. Python has strptime() method which parses a string representing a time according to a format. If string cannot be parsed according to format, or if it has excess data after parsing, ValueError is raised.

Here is the code:

import time

given_time = "10:30:45"
try:
    time.strptime(given_time, '%H:%M:%S')
    print("Correct Format")
except ValueError:
    print("Incorrect Format")

Here are its formats:

  • %a - abbreviated weekday name

  • %A - full weekday name

  • %b - abbreviated month name

  • %B - full month name

  • %c - preferred date and time representation

  • %C - century number (the year divided by 100, range 00 to 99)

  • %d - day of the month (01 to 31)

  • %D - same as %m/%d/%y

  • %e - day of the month (1 to 31)

  • %g - like %G, but without the century

  • %G - 4-digit year corresponding to the ISO week number (see %V).

  • %h - same as %b

  • %H - hour, using a 24-hour clock (00 to 23)

  • %I - hour, using a 12-hour clock (01 to 12)

  • %j - day of the year (001 to 366)

  • %m - month (01 to 12)

  • %M - minute

  • %n - newline character

  • %p - either am or pm according to the given time value

  • %r - time in a.m. and p.m. notation

  • %R - time in 24 hour notation

  • %S - second

  • %t - tab character

  • %T - current time, equal to %H:%M:%S

  • %u - weekday as a number (1 to 7), Monday=1. Warning: In Sun Solaris Sunday=1

  • %U - week number of the current year, starting with the first Sunday as the first day of the first week

  • %V - The ISO 8601 week number of the current year (01 to 53), where week 1 is the first week that has at least 4 days in the current year, and with Monday as the first day of the week

  • %W - week number of the current year, starting with the first Monday as the first day of the first week

  • %w - day of the week as a decimal, Sunday=0

  • %x - preferred date representation without the time

  • %X - preferred time representation without the date

  • %y - year without a century (range 00 to 99)

  • %Y - year including the century

  • %Z or %z - time zone or name or abbreviation

  • %% - a literal % character

Sign up to request clarification or add additional context in comments.

1 Comment

If you need to check many lines, you need to build a high ladder from try-catch blocks. I don't think this is a good pattern.
2

You can use regex to match the format, then take action depending on if a match is found. Provide re.match() with a pattern and the string you want to check:

import re

strings = ["8:30:00", "16:00:00", "845:00", "aa:bb:00"]

for s in strings:
    if re.match("\d{1,2}:\d{2}:\d{2}", s):  # Will return True if pattern matches s
        print("match: {}".format(s))  # Take action on a matching pattern
    else:
        print("no match: {}".format(s))

The pattern \d{1,2}:\d{2}:\d{2} will match 1 or 2 digits, colon, 2 digits, colon, and 2 digits

The above will print:

match: 8:30:00
match: 16:00:00
no match: 845:00
no match: aa:bb:00

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.