Regex MM:SS, unlimited minutes (no hours)

Viewed 50

I'm using some validation in Google Sheets but I need the total minutes and seconds in the following format for each dive.

E.g.

42:38 (42 minutes, 38 seconds)
62:35
85:26

I do not want hours to be an option; I only want to work with minutes and seconds, as this is then converted in the database separately.

I have seen the below on another form but have no idea where to start with regex:

^(?:(?:([01]?\d|2[0-3]):)?([0-5]?\d):)?([0-5]?\d)$

This gives HH:MM:SS (with HH being optional); I have tried to have a play around with it but to no avail.

1 Answers

You can use

^(\d+):([0-5]?\d)$

Or, if \d is not supported as in POSIX ERE:

^([0-9]+):([0-5]?[0-9])$

See the regex demo. Details:

  • ^ - start of string
  • (\d+) - Group 1: one or more digits
  • : - a colon
  • ([0-5]?\d) - Group 2: a digit from 0 to 5 and then one digit
  • $ - end of string.
Related