正时HH:MM:SS时间字符串的正则表达式模式 [英] Regex pattern for EXACTLY HH:MM:SS time String
问题描述
我想用确切的 hh:mm:ss
字符串验证字符串时间格式。
我的意思是完全相同的
小时/分钟/秒中的每个小时必须为 2位数字
还必须是
仅接受逻辑值,例如
I want to validate sting time format in EXACTLY hh:mm:ss
String.
I mean by EXACTLY that
Each of hours / minutes / seconds MUST be 2 digits
Also
Accept only logical values like
- 小时[从00到23]
- 分钟[从00到59]
- 秒[从00到59]
当我选中 HH:MM:SS时间字符串的正则表达式模式
答案接受 hh:mm:ss
字符串,但也接受类似 2:3:24
When i checked Regex pattern for HH:MM:SS time string
The answer accept hh:mm:ss
string but also accepts cases like 2:3:24
的情况
推荐答案
您的正则表达式将是
(?:[01]\d|2[0123]):(?:[012345]\d):(?:[012345]\d)
这将匹配 20:30:30和 2020-05-29 20:30:30 -0600。
This will match both "20:30:30" and "2020-05-29 20:30:30 -0600".
如果您只想匹配专有的字符串 24小时,请使用以下命令:
If you want to only match Strings that are exclusively 24-hour times, use the following:
^(?:[01]\d|2[0123]):(?:[012345]\d):(?:[012345]\d)$
这将仅匹配 20:30:30,而不匹配 2020-05-29 20:30:30 -0600。
This will match only "20:30:30" and not "2020-05-29 20:30:30 -0600".
Java正则表达式为
Java regex would be,
(?:[01]\\d|2[0123]):(?:[012345]\\d):(?:[012345]\\d)
对于仅24小时的字符串,
And for exclusively 24-hour Strings,
^(?:[01]\\d|2[0123]):(?:[012345]\\d):(?:[012345]\\d)$
这篇关于正时HH:MM:SS时间字符串的正则表达式模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!