0

I am using following regular expression to accept the time in 24hr format in Objective-C.

(^([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?

4

1 回答 1

2

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. It doesn't accept empty strings as the groups are not optional.

于 2012-12-26T11:18:59.410 回答