REGEX of HH:MM:SS may be negative

Asked

Viewed 47 times

4

I’m working with HH:MM:SS and I have this REGEX ^([0-1]?\\d{0,4})(?::([0-5]?\\d))?(?::([0-5]?\\d))?$ I’m using the library Inputmask but I would like to work with negative hours example:

Effort: -200:00:00

I can continue using this regex or have to use another?

  • 2

    Utilize -? to indicate that the hyphen at the beginning is optional. ^-?(?:[0-9]{0,4}):(?:[0-5][0-9]):(?:[0-5][0-9])$

  • It worked thanks @Valdeirpsr

  • @Valdeirpsr now only post as response to win accepted :)

1 answer

4


Utilize -? at the beginning of the REGEX for the negative signal to be optional:

 ^-?(?:[0-9]{0,4}):(?:[0-5][0-9]):(?:[0-5][0-9])$

Browser other questions tagged

You are not signed in. Login or sign up in order to post.