Reputation: 179
I've got an HTML text input with a Parsley JS pattern validation where I'm hoping to validate a day hour minute string in the pattern of 99d 99m 99h. I've got the regex working for the entire string (all three segments present (d m h)) but I'd like it to also validate if only one or two of the three segments are present (00d 00h or 00h 00m or 00h or 00m or 00d).
Here's my pattern:
(?:(\d+d)\s)?(?:(\d+h)\s)?(?:(\d+m))
Is there a way to group these so the other five patterns also match without writing several more pattern groups?
(?:(\d+d)\s)?(?:(\d+h)\s)?(?:(\d+m))?:(?:(\d+d)\s)?(?:(\d+h)\s))?:(?:(\d+h)\s)?(?:(\d+m)?(\d+d)\s)?(\d+h)\s)?(\d+m)\s)
Upvotes: 3
Views: 417
Reputation: 83
I may not be 100% on what your needs are, but couldn't you do some version of what I have below here, adding in some whitespace patterns or something?
^(\d{2}[dhm]){1,3}$
Edit based on comments What about this version?
^(\d{2}d\s\d{2}h\s\d{2}m)|(\d{2}[dh]\s\d{2}m)|(\d{2}d\s\d{2}[hm])|(\d{2}[dhm])$
Upvotes: 0