用于匹配HH:MM时间格式的正则表达式

时间:2022-01-12 01:50:43

I want a regexp for matching time in HH:MM format. Here's what I have, and it works:

我想要一个regexp来匹配HH:MM格式的时间。这是我所拥有的,它是有效的:

^[0-2][0-3]:[0-5][0-9]$

This matches everything from 00:00 to 23:59.

从00:00到23:59,这一切都匹配。

However, I want to change it so 0:00 and 1:00, etc are also matched as well as 00:00 and 01:30. I.e to make the leftmost digit optional, to match HH:MM as well as H:MM.

但是,我想把它改成0:00和1:00等也匹配,00:00和01:30也匹配。我。e使最左边的数字可选,匹配HH:MM以及H:MM。

Any ideas how to make that change? I need this to work in javascript as well as php.

有什么办法改变吗?我需要在javascript和php中使用它。

17 个解决方案

#1


254  

Your original regular expression has flaws: it wouldn't match 04:00 for example.

你原来的正则表达式有缺陷:比如它不能匹配04:00。

This may work better:

这可能效果更好:

^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$

#2


35  

None of the above worked for me. In the end I used:

以上这些对我都不起作用。最后我用了:

^([0-1]?[0-9]|2[0-3]):[0-5][0-9]$ (js engine)

Logic:

逻辑:

The first number (hours) is either: a number between 0 and 19 --> [0-1]?[0-9] (allowing single digit number)
or
a number between 20 - 23 --> 2[0-3]

第一个数字(小时)是:0到19之间的数字——> [0-1]?[0-9](允许个位数)或20 - 23 -> 2[0-3]

the second number (minutes) is always a number between 00 and 59 --> [0-5][0-9] (not allowing a single digit)

第二个数字(分钟)总是在00到59之间——>[0-5][0-9](不允许一个数字)

#3


4  

The best would be for HH:MM without taking any risk.

最好的办法是不冒任何风险。

^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$

#4


4  

You can use this one 24H, seconds are optional

你可以使用这个24H,秒是可选的。

^([0-1]?[0-9]|[2][0-3]):([0-5][0-9])(:[0-5][0-9])?$

#5


2  

Amazingly I found actually all of these don't quite cover it, as they don't work for shorter format midnight of 0:0 and a few don't work for 00:00 either, I used and tested the following:

令人惊讶的是,我发现实际上所有这些都没有涵盖它,因为它们不适用于较短的0:0的午夜格式,也有一些不适用于00:00,我使用并测试了以下内容:

^([0-9]|0[0-9]|1?[0-9]|2[0-3]):[0-5]?[0-9]$

#6


2  

You can use this regular expression:

您可以使用这个正则表达式:

^(2[0-3]|[01]?[0-9]):([1-5]{1}[0-9])$

If you want to exclude 00:00, you can use this expression

如果您想排除00:00,可以使用这个表达式

^(2[0-3]|[01]?[0-9]):(0[1-9]{1}|[1-5]{1}[0-9])$

Second expression is better option because valid time is 00:01 to 00:59 or 0:01 to 23:59. You can use any of these upon your requirement. Regex101 link

第二个表达式是更好的选择,因为有效时间是00:01到00:59或0:01到23:59。您可以根据您的要求使用其中任何一个。Regex101链接

#7


0  

You can use following regex:

您可以使用以下regex:

^[0-1][0-9]:[0-5][0-9]$|^[2][0-3]:[0-5][0-9]$|^[2][3]:[0][0]$

#8


0  

Declare

声明

private static final String TIME24HOURS_PATTERN = "([01]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]";

public boolean validate(final String time) {
    pattern = Pattern.compile(TIME24HOURS_PATTERN);
    matcher = pattern.matcher(time);
    return matcher.matches();
}

This method return "true" when String match with the Regular Expression.

当字符串与正则表达式匹配时,此方法返回“true”。

#9


0  

Matches:

匹配:

2:9
2:09
2:59
02:9
02:09
02:59
23:59

Use it:

使用它:

^([0-9]|0[0-9]|1[0-9]|2[0-3]):([0-9]|[0-5][0-9])$

#10


0  

As you asked the left most bit optional, I have done left most and right most bit optional too, check it out

当你问左边的大部分是可选的,我已经做了最左边和右边的大部分也是可选的,检查它。

^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]?$

It matches with

与它匹配

0:0 
00:00
00:0 
0:00
23:59
01:00
00:59

The live link is available here

这里有实时链接

#11


0  

A slight modification to Manish M Demblani's contribution above handles 4am (I got rid of the seconds section as I don't need it in my application)

稍微修改一下Manish M Demblani在上面的贡献(我去掉了seconds部分,因为我的应用不需要它)

^(([0-1]{0,1}[0-9]( )?(AM|am|aM|Am|PM|pm|pM|Pm))|(([0]?[1-9]|1[0-2])(:|\.)[0-5][0-9]( )?(AM|am|aM|Am|PM|pm|pM|Pm))|(([0]?[0-9]|1[0-9]|2[0-3])(:|\.)[0-5][0-9]))$

handles: 4am 4 am 4:00 4:00am 4:00 pm 4.30 am etc..

手柄:凌晨4点4点4点4点4点4点4点30分等

#12


0  

The below regex will help to validate hh:mm format

下面的regex将有助于验证hh:mm格式

^([0-1][0-9]|2[0-3]):[0-5][0-9]$

#13


-1  

Mine is:

我的是:

^(1?[0-9]|2[0-3]):[0-5][0-9]$

^(1 ?[0 - 9]| 2(0 - 3)):美元[0 - 9][0 - 5]

This is much shorter

这是更短

Got it tested with several example

用几个例子来测试它。

Match:

匹配:

  • 00:00
  • 00:00
  • 7:43
  • 43
  • 07:43
  • 07:43
  • 19:00
  • 晚7:00
  • 18:23
  • 23

And doesn't match any invalid instance such as 25:76 etc ...

并且不匹配任何无效的实例,例如25:76等等……

#14


-1  

You can try the following

您可以尝试以下方法

^\d{1,2}([:.]?\d{1,2})?([ ]?[a|p]m)?$

It can detect the following patterns :

它可以检测到以下模式:

2300 
23:00 
4 am 
4am 
4pm 
4 pm
04:30pm 
04:30 pm 
4:30pm 
4:30 pm
04.30pm
04.30 pm
4.30pm
4.30 pm
23:59 
0000 
00:00

#15


-1  

check this masterfull timestamp detector regex I built to look for a user-specified timestamp, examples of what it will pickup include, but is most definitely NOT limited to;

检查这个masterfull timestamp检测器regex,我构建它是为了查找用户指定的时间戳,它将包括哪些内容,但绝对不限于;

8:30-9:40
09:40-09 : 50
09 : 40-09 : 50
09:40 - 09 : 50
08:00to05:00
08 : 00to05 : 00
08:00 to 05:00
8am-09pm
08h00 till 17h00
8pm-5am
08h00,21h00
06pm untill 9am

It'll also pickup many more, as long as the times include digits

只要时间包含数字,它还会增加很多

#16


-2  

You can use following regex :

您可以使用以下regex:

^[0-2]?[0-3]:[0-5][0-9]$

Only modification I have made is leftmost digit is optional. Rest of the regex is same.

只有我做的修改是最左边的数字是可选的。regex的其余部分是相同的。

#17


-3  

Try the following

试试以下

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

Note: I was assuming the javascript regex engine. If it's different than that please let me know.

注意:我假设是javascript regex引擎。如果有什么不同,请告诉我。

#1


254  

Your original regular expression has flaws: it wouldn't match 04:00 for example.

你原来的正则表达式有缺陷:比如它不能匹配04:00。

This may work better:

这可能效果更好:

^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$

#2


35  

None of the above worked for me. In the end I used:

以上这些对我都不起作用。最后我用了:

^([0-1]?[0-9]|2[0-3]):[0-5][0-9]$ (js engine)

Logic:

逻辑:

The first number (hours) is either: a number between 0 and 19 --> [0-1]?[0-9] (allowing single digit number)
or
a number between 20 - 23 --> 2[0-3]

第一个数字(小时)是:0到19之间的数字——> [0-1]?[0-9](允许个位数)或20 - 23 -> 2[0-3]

the second number (minutes) is always a number between 00 and 59 --> [0-5][0-9] (not allowing a single digit)

第二个数字(分钟)总是在00到59之间——>[0-5][0-9](不允许一个数字)

#3


4  

The best would be for HH:MM without taking any risk.

最好的办法是不冒任何风险。

^(0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$

#4


4  

You can use this one 24H, seconds are optional

你可以使用这个24H,秒是可选的。

^([0-1]?[0-9]|[2][0-3]):([0-5][0-9])(:[0-5][0-9])?$

#5


2  

Amazingly I found actually all of these don't quite cover it, as they don't work for shorter format midnight of 0:0 and a few don't work for 00:00 either, I used and tested the following:

令人惊讶的是,我发现实际上所有这些都没有涵盖它,因为它们不适用于较短的0:0的午夜格式,也有一些不适用于00:00,我使用并测试了以下内容:

^([0-9]|0[0-9]|1?[0-9]|2[0-3]):[0-5]?[0-9]$

#6


2  

You can use this regular expression:

您可以使用这个正则表达式:

^(2[0-3]|[01]?[0-9]):([1-5]{1}[0-9])$

If you want to exclude 00:00, you can use this expression

如果您想排除00:00,可以使用这个表达式

^(2[0-3]|[01]?[0-9]):(0[1-9]{1}|[1-5]{1}[0-9])$

Second expression is better option because valid time is 00:01 to 00:59 or 0:01 to 23:59. You can use any of these upon your requirement. Regex101 link

第二个表达式是更好的选择,因为有效时间是00:01到00:59或0:01到23:59。您可以根据您的要求使用其中任何一个。Regex101链接

#7


0  

You can use following regex:

您可以使用以下regex:

^[0-1][0-9]:[0-5][0-9]$|^[2][0-3]:[0-5][0-9]$|^[2][3]:[0][0]$

#8


0  

Declare

声明

private static final String TIME24HOURS_PATTERN = "([01]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]";

public boolean validate(final String time) {
    pattern = Pattern.compile(TIME24HOURS_PATTERN);
    matcher = pattern.matcher(time);
    return matcher.matches();
}

This method return "true" when String match with the Regular Expression.

当字符串与正则表达式匹配时,此方法返回“true”。

#9


0  

Matches:

匹配:

2:9
2:09
2:59
02:9
02:09
02:59
23:59

Use it:

使用它:

^([0-9]|0[0-9]|1[0-9]|2[0-3]):([0-9]|[0-5][0-9])$

#10


0  

As you asked the left most bit optional, I have done left most and right most bit optional too, check it out

当你问左边的大部分是可选的,我已经做了最左边和右边的大部分也是可选的,检查它。

^([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]?$

It matches with

与它匹配

0:0 
00:00
00:0 
0:00
23:59
01:00
00:59

The live link is available here

这里有实时链接

#11


0  

A slight modification to Manish M Demblani's contribution above handles 4am (I got rid of the seconds section as I don't need it in my application)

稍微修改一下Manish M Demblani在上面的贡献(我去掉了seconds部分,因为我的应用不需要它)

^(([0-1]{0,1}[0-9]( )?(AM|am|aM|Am|PM|pm|pM|Pm))|(([0]?[1-9]|1[0-2])(:|\.)[0-5][0-9]( )?(AM|am|aM|Am|PM|pm|pM|Pm))|(([0]?[0-9]|1[0-9]|2[0-3])(:|\.)[0-5][0-9]))$

handles: 4am 4 am 4:00 4:00am 4:00 pm 4.30 am etc..

手柄:凌晨4点4点4点4点4点4点4点30分等

#12


0  

The below regex will help to validate hh:mm format

下面的regex将有助于验证hh:mm格式

^([0-1][0-9]|2[0-3]):[0-5][0-9]$

#13


-1  

Mine is:

我的是:

^(1?[0-9]|2[0-3]):[0-5][0-9]$

^(1 ?[0 - 9]| 2(0 - 3)):美元[0 - 9][0 - 5]

This is much shorter

这是更短

Got it tested with several example

用几个例子来测试它。

Match:

匹配:

  • 00:00
  • 00:00
  • 7:43
  • 43
  • 07:43
  • 07:43
  • 19:00
  • 晚7:00
  • 18:23
  • 23

And doesn't match any invalid instance such as 25:76 etc ...

并且不匹配任何无效的实例,例如25:76等等……

#14


-1  

You can try the following

您可以尝试以下方法

^\d{1,2}([:.]?\d{1,2})?([ ]?[a|p]m)?$

It can detect the following patterns :

它可以检测到以下模式:

2300 
23:00 
4 am 
4am 
4pm 
4 pm
04:30pm 
04:30 pm 
4:30pm 
4:30 pm
04.30pm
04.30 pm
4.30pm
4.30 pm
23:59 
0000 
00:00

#15


-1  

check this masterfull timestamp detector regex I built to look for a user-specified timestamp, examples of what it will pickup include, but is most definitely NOT limited to;

检查这个masterfull timestamp检测器regex,我构建它是为了查找用户指定的时间戳,它将包括哪些内容,但绝对不限于;

8:30-9:40
09:40-09 : 50
09 : 40-09 : 50
09:40 - 09 : 50
08:00to05:00
08 : 00to05 : 00
08:00 to 05:00
8am-09pm
08h00 till 17h00
8pm-5am
08h00,21h00
06pm untill 9am

It'll also pickup many more, as long as the times include digits

只要时间包含数字,它还会增加很多

#16


-2  

You can use following regex :

您可以使用以下regex:

^[0-2]?[0-3]:[0-5][0-9]$

Only modification I have made is leftmost digit is optional. Rest of the regex is same.

只有我做的修改是最左边的数字是可选的。regex的其余部分是相同的。

#17


-3  

Try the following

试试以下

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

Note: I was assuming the javascript regex engine. If it's different than that please let me know.

注意:我假设是javascript regex引擎。如果有什么不同,请告诉我。