python时间格式检查 [英] python time format check

查看:29
本文介绍了python时间格式检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 python 中,我想检查输入字符串是否在HH:MM"中,例如 01:16 或 23:16 或 24:00.根据结果​​给出真假.

At python, I want to check if the input string is in "HH:MM" such as 01:16 or 23:16 or 24:00. Giving true or false by the result.

如何使用正则表达式来实现这一点?

How can I achieve this by using regular expression ?

推荐答案

无需正则表达式即可实现:

You can achieve this without regular expressions:

import time

def isTimeFormat(input):
    try:
        time.strptime(input, '%H:%M')
        return True
    except ValueError:
        return False

>>>isTimeFormat('12:12')
True

>>>isTimeFormat('012:12')
False

这篇关于python时间格式检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆