Reputation: 1625
I've got the following regex:
^(?:(?:([01]?\d|2[0-3]):)?([0-5]?\d):)?([0-5]?\d)$
This currently will accept 00:00:00
or 00:00
or 00
I don't want it do this.
It can accept 00:01
or 00:00:01
but not all zeros.
What have I missed in the regex?
Upvotes: 1
Views: 158
Reputation: 52185
I think it might be easier to have another regex which will match whatever it is you do not want.
Something like so: ^(00:?)+$
should match what you do not want. Simply use that check before you use your current regex. Doing everything in one regular expression can be messy to build and change.
Upvotes: 0
Reputation: 2749
You can use negative lookaheads to solve this:
^(?!^(\D|0)*$)(?:(?:([01]?\d|2[0-3]):)?([0-5]?\d):)?([0-5]?\d)$
The negative lookahead that matches nondigits and zeroes. Thus if you have only zeroes in your time, it will fail to match.
You can see what it does in more detail on www.debuggex.com.
Upvotes: 3