要用正则表达式精准匹配时间,其实并不容易@H_301_2@
方式一:@H_301_2@
>>> import re
>>> t = '19:10:48'
>>> m = re.match(r'(.*):(.*):(.*)',t)
>>> m.groups()
('19','10','48')
@H_301_2@
方式二:@H_301_2@
>>> t = '19:10:48'
>>> m = re.match(r'(\d{2}):(\d{2}):(\d{2})','48')
@H_301_2@
比如上面的并不能精准匹配,比如说,24:61:61显然不符合要求。@H_301_2@
时(H),0-23@H_301_2@
分(M),0-59@H_301_2@
秒(S),0-59@H_301_2@
小时的精准匹配如下: @H_301_2@0?[0-9]|1[0-9]|2[0-3]
分钟的精准匹配如下: @H_301_2@0?[0-9]|[1-5][0-9]@H_301_2@
秒钟的精准匹配如下: @H_301_2@0?[0-9]|[1-5][0-9]@H_301_2@
>>> t = '23:59:08' >>> p = re.compile(r'^(0?[0-9]|1[0-9]|2[0-3]):(0?[0-9]|[1-5][0-9]):(0?[0-9]|[1-5][0-9])$') >>> s = p.search(t) >>> s.groups() ('23','59','08') @H_301_2@