doudizhi947129 2012-07-02 15:20
浏览 25
已采纳

正则表达式用于时间验证

I want to validate a time with a regex. I created the following expression :

'#^([01][0-9])|(2[0-4])(:[0-5][0-9]){1,2}$#'

Here is the problem:

<?php
var_dump(preg_match('#^([01][0-9])|(2[0-4])(:[0-5][0-9]){1,2}$#', '14:25'));
// Returns 1 (OK)

var_dump(preg_match('#^([01][0-9])|(2[0-4])(:[0-5][0-9]){1,2}$#', '25:25'));
// Returns 0 (OK)

var_dump(preg_match('#^([01][0-9])|(2[0-4])(:[0-5][0-9]){1,2}$#', '14:2555'));
// Returns 1 (instead of 0 as I would like to get)
?>

Does anybody know what is wrong?

  • 写回答

4条回答 默认 最新

  • dqpu4988 2012-07-02 15:30
    关注

    Time in 24-Hour format regular expression pattern :

    ([01]?[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?
    

    The 24-hour clock format is start from 0-23 or 00-23 then a semi colon (:) and follow by 00-59 then (optionally a semi colon (:) and follow by 00-59).

    Description :

    (                  # start of group #1
      [01]?[0-9]       # start with 0-9,1-9,00-09,10-19
      |                # or
      2[0-3]           # start with 20-23
    )                  # end of group #1
    :                  # follow by a semi colon (:)
    [0-5][0-9]         # follow by 0..5 and 0..9, which means 00 to 59
    (                  # start of group #2
      :                # follow by a semi colon (:)
      [0-5][0-9]       # follow by 0..5 and 0..9, which means 00 to 59
    )                  # end of group #2
    ?                  # optional third part
    

    Matching time formats :

    01:00, 02:00, 13:00,
     1:00,  2:00, 13:01,
    23:59, 15:00,
    00:00,  0:00,
    14:34:43, 01:00:00
    

    Not matching time formats :

     24:00             # hour is out of range [0-23]
     12:60             # minute is out of range [00-59]
      0:0              # invalid format for minute, at least 2 digits
     13:1              # invalid format for minute, at least 2 digits
      0:00:0           # invalid format for seconds, at least 2 digits
    101:00             # hour is out of range [0-23]
    

    Example :

    var_dump(preg_match('#^[01]?[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?$#', '14:25'));    // OK
    var_dump(preg_match('#^[01]?[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?$#', '25:25'));    // KO
    var_dump(preg_match('#^[01]?[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?$#', '25:30'));    // KO
    var_dump(preg_match('#^[01]?[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?$#', '14:2555'));  // KO
    var_dump(preg_match('#^[01]?[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?$#', '14:65'));    // KO
    var_dump(preg_match('#^[01]?[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?$#', '14:59'));    // OK
    var_dump(preg_match('#^[01]?[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?$#', '14:34:43')); // OK
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题