GG_Python
GG_Python

Reputation: 3541

Regex for Daily Time (with zone) or Time-Interval

All, a regex for a form validation function is needed to determine whether a user's input for a daily time (24h format, always with the timezone) or an interval is OK. There are 5 possible time zones (4 continental US + UTC as suffixes), and 2 interval types (can be float, h or m suffixes). A few examples for valid inputs are

Accepted answer here was a good start, but i couldn't quite get the time zone suffix regex to work, or to add an alternation for the interval case.

For reference, required timezones would be EST,CST,MST,PST and UTC. Or course, any example with a couple of time zones is enough, can always be extended if other SO readers find it useful.

Any ideas?

Upvotes: 2

Views: 1532

Answers (1)

Sebastian Proske
Sebastian Proske

Reputation: 8413

Using the sample from your link, I added a check for the timezones and the alternation for the intervals, here you go:

^(?:(?:[01]?[0-9]|2[0-3]):[0-5][0-9](?:[ECMP]ST|UTC)|\d+(?:\.\d+)?[mh])$
  • [ECMP]ST|UTC matches E, C, M or P followed by ST or matches UTC
  • \d+(?:\.\d+)? matches one or more digits, optionally followed by a dot and more digits
  • [mh] matches m or h literally

https://regex101.com/r/cR3xA4/4

Upvotes: 2

Related Questions