How could you match 12 hour time in a regex-- in other words match 12:30 but not 14:74? Thanks!
如何在regex中匹配12小时的时间——换句话说,匹配12:30而不是14:74?谢谢!
11 个解决方案
#1
17
This should work:
这应该工作:
([1-9]|1[012]):[0-5][0-9]
#2
16
This is an example of a problem where "hey I know, I'll use regular expressions!" is the wrong solution. You can use a regular expression to check that your input format is digit-digit-colon-digit-digit, then use programming logic to ensure that the values are within the range you expect. For example:
这是一个问题的例子,“嘿,我知道,我将使用正则表达式!”是错误的解决方案。可以使用正则表达式检查输入格式是否为digit-digit-colon-digit- digital,然后使用编程逻辑确保值在您期望的范围内。例如:
/(\d\d?):(\d\d)/
if ($1 >= 1 && $1 <= 12 && $2 < 60) {
// result is valid 12-hour time
}
This is much easier to read and understand than some of the obfuscated regex examples you see in other answers here.
这比您在其他答案中看到的一些模糊的regex示例更容易阅读和理解。
#3
6
^(00|0[0-9]|1[012]):[0-5][0-9] ?((a|p)m|(A|P)M)$
^
- Match the beginning of the string.
^ -匹配字符串的开始。
(00|0[0-9]|1[012])
- any two-digit number up to 12. Require two digits.
(00|0[0-9]|1[012]) -可达12的任何两位数。需要两个数字。
:
- Match a colon
-匹配冒号
[0-5][0-9]
- Match any two-digit number from 00 to 59.
[0-5][0-9] -匹配任何从00到59的两位数。
?
- Match a space zero or one times.
吗?-匹配空格0或1次。
((a|p)m|(A|P)M)
- Match am or pm, case insensitive.
(a|p)m|(a|p)m) -匹配am或pm,不区分大小写。
$
- Match the end of the string.
$ -匹配字符串的末尾。
#4
1
Like this: ((?:1[0-2]|0\d)\:(?:[0-5]\d))
if you want leading 0 for the hour, ((?:1[0-2]|\d)\:(?:[0-5]\d))
if you don't and ((?:1[0-2]|0?\d)\:(?:[0-5]\d))
if you don't care.
像这样:((?):1(0 - 2)| 0 \ d)\:(?:(0 - 5)\ d))如果你想要领先0小时,((?:1(0 - 2)| \ d)\:(?:(0 - 5)\ d))如果你不,((?:1(0 - 2)| 0 ? \ d)\:(?:(0 - 5)\ d))如果你不在乎。
#5
1
why regex? you can do this will simple integer check
为什么正则表达式?你可以做这个简单的整数检查
$str = "12:74";
list($h , $m ) = explode(":",$str);
if ( ($h <=12 && $h >=0 ) && ($m <=59 && $m >=0) ) {
print "Time Ok.";
}else{
print "Time not ok";
}
#6
0
I believe the above fail in at least one way, particularly regarding strings such as "13:00" (Keith's matches "3:00" in that case).
我认为上面的方法至少有一种失败的地方,尤其是关于字符串,比如“13:00”(在这种情况下,基思的匹配是“3:00”)。
This one should handle that issue as well as the others brought up.
这个问题应该和其他提出的问题一样得到解决。
([01][0-2]|(?<!1)[0-9]):([0-5][0-9])
#7
0
(0?\d|1[0-2]):([0-5]\d)
That will match everything from 0:00 up to 12:59. That's 13 hours, by the way. If you don't want to match 0:00 - 0:59, try this instead:
这将匹配从0:00到12:59的一切。顺便说一下,那是13个小时。如果你不想匹配0:00 - 0:59,试试这个:
([1-9]|1[0-2]):([0-5]\d)
#8
0
You could use this one:
你可以用这个:
/((?:1[0-2])|(?:0?[0-9])):([0-5][0-9]) ?([ap]m)/
/1 => hour
/2 => minute
/3 => am/pm
#9
0
The following matches padded and non-padded hours in a 24hr clock (i.e. 00:00 - 23:59) between 00:00 and 12:59.
在00:00到12:59之间,在24小时(即00:00 - 23:59)的时间内(即00:00 - 23:59),以下是填充和非填充的时间。
(?:(?<!\d)[0-9]|0[0-9]|1[0-2]):[0-5][0-9]
Matches:
匹配:
- 00:00
- 00:00
- 00:58
- 00:58
- 01:34
- 01:34
- 1:34
- 34
- 8:35
- 35
- 12:23
- 12:23
- 12:59
- 59
Nonmatches:
Nonmatches:
- 13:00
- 13:00
- 13:23
- 13:23
- 14:45
- 45
- 23:59
- 23:59
#10
0
^(?:(?:1?(?:[0-2]))|[1-9]):[0-5][0-9]
#11
0
Here is a 12 hour pattern with AM and PM validation.
这里有一个12小时的模式和AM和PM验证。
TIME12HOURSAMPM_PATTERN = "^(?:(?<!\\d)[0-9]|0[0-9]|1[0-2]):[0-5][0-9] ?((a|p)m|(A|P)M)$";
TIME12HOURSAMPM_PATTERN = " ^(?:(? < ! \ \ d)[0 - 9][0 - 9]| 0 | 1[0]):[0 - 9][0 - 5]?(p(|)m | | p m)美元”;
#1
17
This should work:
这应该工作:
([1-9]|1[012]):[0-5][0-9]
#2
16
This is an example of a problem where "hey I know, I'll use regular expressions!" is the wrong solution. You can use a regular expression to check that your input format is digit-digit-colon-digit-digit, then use programming logic to ensure that the values are within the range you expect. For example:
这是一个问题的例子,“嘿,我知道,我将使用正则表达式!”是错误的解决方案。可以使用正则表达式检查输入格式是否为digit-digit-colon-digit- digital,然后使用编程逻辑确保值在您期望的范围内。例如:
/(\d\d?):(\d\d)/
if ($1 >= 1 && $1 <= 12 && $2 < 60) {
// result is valid 12-hour time
}
This is much easier to read and understand than some of the obfuscated regex examples you see in other answers here.
这比您在其他答案中看到的一些模糊的regex示例更容易阅读和理解。
#3
6
^(00|0[0-9]|1[012]):[0-5][0-9] ?((a|p)m|(A|P)M)$
^
- Match the beginning of the string.
^ -匹配字符串的开始。
(00|0[0-9]|1[012])
- any two-digit number up to 12. Require two digits.
(00|0[0-9]|1[012]) -可达12的任何两位数。需要两个数字。
:
- Match a colon
-匹配冒号
[0-5][0-9]
- Match any two-digit number from 00 to 59.
[0-5][0-9] -匹配任何从00到59的两位数。
?
- Match a space zero or one times.
吗?-匹配空格0或1次。
((a|p)m|(A|P)M)
- Match am or pm, case insensitive.
(a|p)m|(a|p)m) -匹配am或pm,不区分大小写。
$
- Match the end of the string.
$ -匹配字符串的末尾。
#4
1
Like this: ((?:1[0-2]|0\d)\:(?:[0-5]\d))
if you want leading 0 for the hour, ((?:1[0-2]|\d)\:(?:[0-5]\d))
if you don't and ((?:1[0-2]|0?\d)\:(?:[0-5]\d))
if you don't care.
像这样:((?):1(0 - 2)| 0 \ d)\:(?:(0 - 5)\ d))如果你想要领先0小时,((?:1(0 - 2)| \ d)\:(?:(0 - 5)\ d))如果你不,((?:1(0 - 2)| 0 ? \ d)\:(?:(0 - 5)\ d))如果你不在乎。
#5
1
why regex? you can do this will simple integer check
为什么正则表达式?你可以做这个简单的整数检查
$str = "12:74";
list($h , $m ) = explode(":",$str);
if ( ($h <=12 && $h >=0 ) && ($m <=59 && $m >=0) ) {
print "Time Ok.";
}else{
print "Time not ok";
}
#6
0
I believe the above fail in at least one way, particularly regarding strings such as "13:00" (Keith's matches "3:00" in that case).
我认为上面的方法至少有一种失败的地方,尤其是关于字符串,比如“13:00”(在这种情况下,基思的匹配是“3:00”)。
This one should handle that issue as well as the others brought up.
这个问题应该和其他提出的问题一样得到解决。
([01][0-2]|(?<!1)[0-9]):([0-5][0-9])
#7
0
(0?\d|1[0-2]):([0-5]\d)
That will match everything from 0:00 up to 12:59. That's 13 hours, by the way. If you don't want to match 0:00 - 0:59, try this instead:
这将匹配从0:00到12:59的一切。顺便说一下,那是13个小时。如果你不想匹配0:00 - 0:59,试试这个:
([1-9]|1[0-2]):([0-5]\d)
#8
0
You could use this one:
你可以用这个:
/((?:1[0-2])|(?:0?[0-9])):([0-5][0-9]) ?([ap]m)/
/1 => hour
/2 => minute
/3 => am/pm
#9
0
The following matches padded and non-padded hours in a 24hr clock (i.e. 00:00 - 23:59) between 00:00 and 12:59.
在00:00到12:59之间,在24小时(即00:00 - 23:59)的时间内(即00:00 - 23:59),以下是填充和非填充的时间。
(?:(?<!\d)[0-9]|0[0-9]|1[0-2]):[0-5][0-9]
Matches:
匹配:
- 00:00
- 00:00
- 00:58
- 00:58
- 01:34
- 01:34
- 1:34
- 34
- 8:35
- 35
- 12:23
- 12:23
- 12:59
- 59
Nonmatches:
Nonmatches:
- 13:00
- 13:00
- 13:23
- 13:23
- 14:45
- 45
- 23:59
- 23:59
#10
0
^(?:(?:1?(?:[0-2]))|[1-9]):[0-5][0-9]
#11
0
Here is a 12 hour pattern with AM and PM validation.
这里有一个12小时的模式和AM和PM验证。
TIME12HOURSAMPM_PATTERN = "^(?:(?<!\\d)[0-9]|0[0-9]|1[0-2]):[0-5][0-9] ?((a|p)m|(A|P)M)$";
TIME12HOURSAMPM_PATTERN = " ^(?:(? < ! \ \ d)[0 - 9][0 - 9]| 0 | 1[0]):[0 - 9][0 - 5]?(p(|)m | | p m)美元”;