java EXACTLY HH:MM:SS 时间字符串的正则表达式模式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25873636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 08:49:56  来源:igfitidea点击:

Regex pattern for EXACTLY HH:MM:SS time String

javaregexvalidationdatetime

提问by ahmednabil88

I want to validate sting time format in EXACTLY hh:mm:ssString.
I mean by EXACTLY that
Each of hours / minutes / seconds MUST be 2 digits
Also Accept only logical valueslike

我想在 EXACTLY hh:mm:ssString 中验证刺痛时间格式。
我的意思是确切地说,
每个小时/分钟/秒都必须是2 位数字
只接受逻辑值,例如

  • hours [ from 00 to 23 ]
  • minutes [ from 00 to 59 ]
  • seconds [ from 00 to 59 ]
  • 小时 [从 00 到 23]
  • 分钟 [从 00 到 59]
  • 秒 [从 00 到 59]

When i checked Regex pattern for HH:MM:SS time string
The answer accept hh:mm:ssstring but also accepts cases like 2:3:24

当我检查HH:MM:SS 时间字符串
的正则表达式模式时,答案接受hh:mm:ss字符串但也接受类似的情况2:3:24

Thanks in advance

提前致谢

回答by Avinash Raj

Your regex would be,

你的正则表达式是,

(?:[01]\d|2[0123]):(?:[012345]\d):(?:[012345]\d)

DEMO

演示

Java regex would be,

Java正则表达式将是,

(?:[01]\d|2[0123]):(?:[012345]\d):(?:[012345]\d)

回答by theftprevention

Give this a try:

试试这个:

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

Demo available here.

演示可用在这里。

回答by null

The pattern you want is:

你想要的模式是:

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

which is for HH:MM:SS24hour format.

这是HH:MM:SS24小时制。