Minh Nguyen
Minh Nguyen

Reputation: 181

Regex pattern for time expression string

I want to parse a time expression string (has 4 groups: week(w), day(d), hour(h) and minute(m)). Some examples are:

  1. -3w 4d 2h 1m
  2. +3w 2h 1m
  3. 3d 1m
  4. 1d
  5. ...

I came up with this pattern:

([+-]{0,1})([0-9]+)w?\s?(0?[1-9]|[12][0-9]|3[01])d?\s?([01]?[0-9]|2[0-3])h?\s?([0-5][0-9])m?

But this pattern not work. Please help. Sorry for my english!

Upvotes: 1

Views: 176

Answers (2)

roydukkey
roydukkey

Reputation: 3288

This is what I use for my validation:

 ^(?:(?:0?[0-9]|1[0-2]):[0-5][0-9]\s?(?:(?:[Aa]|[Pp])[Mm])?|(?:1[3-9]|2[0-3]):[0-5][0-9])$

It works for 24:00 and 12:00 format allowing capital am/pm.

Maybe it can help you in the right direction.

Upvotes: 0

vks
vks

Reputation: 67968

^([+-]?)([0-9]+)?w?\s*?(0?[1-9]|[12][0-9]|3[01])?d?\s*?([01]?[0-9]?|2[0-3])?h?\s*?([0-5]?[0-9])?m?$

Modified your regex.Works now.Might need more testing though.

See demo.

http://regex101.com/r/sB4kE6/4

Upvotes: 1

Related Questions