user3319891
user3319891

Reputation: 13

Regular expression syntax for hours

I need a regular expression for hours in PHP, I am using ereg. I need it to accept 1-23 without leading zeros.

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

That's what I am using but I cannot find where is the mistake.

Upvotes: 1

Views: 86

Answers (1)

p.s.w.g
p.s.w.g

Reputation: 149010

Alternations (|) apply to everything in the surrounding group or globally if not in a group. So in your pattern, the ^ only applies to the first pattern and the $ only applies to the last pattern. In other words, your pattern matches any string which begins with a digit from 1 to 9, contains a 1 followed by a digit from 0 to 9, or ends with a 2 followed by a digit from 0 to 3.

Try putting the different options in one group:

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

Also note, 24-hour time starts at 00:00, so your pattern should look more like this:

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

Or this, if you need the hour to be 2 digits:

^([01][0-9]|2[0-3])$

Upvotes: 2

Related Questions