# ^ look at start of line
# ( to start encapsulate to look at hours
# 0? to indicate hours could have a leading 0 and a number
# [] to indicate range of second digit of that number between 0 and 9
# |
# 1 to indicate if first digit is 1
# [] to indicate range of second digit ofthat number between 0 and 9
# |
# 2 to indicate if first digit is 2
# [] to indicate range of second digit of that number between 0 and 9
# ) to close encapsulate
# : to indicate minutes
# [] to indicate 1st digit can be from 0 to 5
# [] to indicate 2nd digit can be from 0 to 9
4条答案
按热度按时间oxcyiej71#
如果您只想接受仅由HH:MM模式组成的 * 行 *,则可以使用行首锚点和行尾锚点,如下所示:
如果您想查找与HH:MM匹配的 words,则可以使用单词边界字符,如下所示:
第一个将匹配“04:20,”但不匹配“04:20am,”也不匹配“04:20am.”第二个将匹配“04:20,”或“04:20am”的“04:20”部分,但不匹配“04:20am.”
qojgxg4l2#
我不明白直到我把它像这样拆开
mwkjh3gx3#
分解:
[01][01]?[0-9]
-〉匹配以0或1开头的两位数小时数,即00-19
AND2[0-3]
-〉匹配小时数20-23
,则:
匹配:
,则[0-5][0-9]
匹配00-59
2w2cym1i4#
我还没查完你的正则表达式呢。
~
似乎有问题,您应该使用,^
作为开始,$
作为结束。if (preg_match_all("^((2[0-3]|[01][1-9]|10):([0-5][0-9]))$", $time)) {
这个应该可以用。