简体   繁体   English

正则表达式+无空字符串

[英]Regular expression + No Empty String

I am using following regular expression to accept the time in 24hr format in Objective-C. 我使用以下正则表达式来接受Objective-C中24小时格式的时间。

(^([1]?[1-9]|2[1-3])?(\\.([0-9]{1,1})?)?$)

It is working perfectly, but it accepts the empty string. 它工作得很好,但它接受空字符串。 I don't want it to accept the empty string. 不希望它接受空字符串。 Could you please guide? 你能指导一下吗?

Your regex seems to be wrong because the part dealing with minutes (the second group) contains just one digit. 你的正则表达式似乎是错误的,因为处理分钟的部分(第二组)只包含一个数字。 I thing the following pattern fits better your needs 我认为以下模式更符合您的需求

^(([01]?\d)|(2[0-3]))\.([0-5]\d)$

The first group deals with hours (that can be in ranges 0-19 or 20-23) and the second group deals with minutes. 第一组处理小时数(可以在0-19或20-23范围内),第二组处理分钟数。 It doesn't accept empty strings as the groups are not optional. 它不接受空字符串,因为这些组不是可选的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM