显然我对正则表达式很可怕,对我来说没有意义......
我想要一个匹配时间的表达式,就像01:23:45在字符串中一样.
我试过这个
(r'(([0-9]*2)[:])*2([0-9]*2)
Run Code Online (Sandbox Code Playgroud)
但它不起作用.我需要能够获得整个时间戳.我试过的其他人只发现了2位数字.
Mar*_*ers 10
你害怕你的中继器错了:
r'\d{2}:\d{2}:\d{2}'
Run Code Online (Sandbox Code Playgroud)
正确的语法是{n,m}最小和最大数字,或{n}完全匹配.该\d字符类也更容易使用[0-9]; 对于常规(非Unicode)匹配,它意味着相同的事情.
为什么还要使用正则表达式 - 使用正确的日期时间函数并免费进行验证......
from datetime import datetime
time = datetime.strptime('01:23:45', '%H:%M:%S').time()
print time.hour, time.minute, time.second
# 1 23 45
Run Code Online (Sandbox Code Playgroud)
达夫时间:
>>> datetime.strptime('99:45:63', '%H:%M:%S')
Traceback (most recent call last):
File "<pyshell#36>", line 1, in <module>
datetime.strptime('99:45:63', '%H:%M:%S')
File "/usr/lib/python2.7/_strptime.py", line 325, in _strptime
(data_string, format))
ValueError: time data '99:45:63' does not match format '%H:%M:%S'
Run Code Online (Sandbox Code Playgroud)