请问这段Python的正则如何理解?
PHP中文网
PHP中文网 2017-04-17 17:55:56
0
5
329
 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表示啥?

PHP中文网
PHP中文网

认证高级PHP讲师

reply all(5)
刘奇

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template