Home  >  Q&A  >  body text

请问这段Python的正则如何理解?

 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中文网2741 days ago309

reply all(5)I'll reply

  • 怪我咯

    怪我咯2017-04-17 17:57:56

    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.

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 17:57:56

    Starts with 0, 1, 2

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 17:57:56

    Match numbers
    Match 00-09
    or 10-19
    or 20-23
    or 0-9

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 17:57:56

    Hours cannot exceed one day, 0-23 hours

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 17:57:56

    0,1,2 are matching numbers

    reply
    0
  • Cancelreply