t = '19:05:30'
m = re.match(r'^(0[0-9]|1[0-9]|2[0-3]|[0-9])\:(0[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9]|[0-9])\:(0[0-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|5[0-9]|[0-9])$', t)
m.groups()
('19', '05', '30')
不理解0[0-9]|1[0-9]|2[0-3]|[0-9]
中括号表示范围我清楚,可是前面的0、1、2表示啥?
It will be very clear if you break the entire paragraph into pieces
(0[0-9]|1[0-9]|2[0-3]|[0-9])
:
(0[0-9]|1[0-9]|2[0 -9]|3[0-9]|4[0-9]|5[0-9]|[0-9])
:
(0[0-9]|1[0-9]|2 [0-9]|3[0-9]|4[0-9]|5[0-9]|[0-9])
Each part does a selection operation, (0[0-9]|1[0-9]|2[0-3]|[0-9]) is to match 0[0-9] or 1[0 -9] or 2[0-3] or [0-9], that is, it matches 00-09 or 10-19 or 20-23 or 0-9, which is all the values of hour.
Other parts are the same.
Starts with 0, 1, 2
Match numbers
Match 00-09
or 10-19
or 20-23
or 0-9
Hours cannot exceed one day, 0-23 hours
0,1,2 are matching numbers