1

I need to determine the gametime of a soccer match. The gametime can always be written as mm:ss. For example, the match starts with 00:00. More importantly, my regex differs from the hour-limit because the official playing time stops at 90:00. So, normally mm is limited to ([0-5]?\d).In addition, I want to make my regex as robust as possible and I want to include the possibility of a lengthy injury-time. In this case, the match can end, for example, at 101:15. The colon-character is always in the middle between the two groups of digits.

With the help of Regular expression for matching HH:MM time format I came up with the following regex:

 ^([0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]$

How should I change this one to include the possibility of a third digit in the first group of digits, (m)mm?

Thanks in advance

HJA24
  • 410
  • 2
  • 11
  • 33
  • What is the maximum range? – The fourth bird Dec 02 '19 at 12:29
  • According to this article the longest match ever lasted 3hours and 23 minutes https://www.theguardian.com/football/2018/mar/29/extra-time-no-end-worlds-longest-match-stockport-doncaster – HJA24 Dec 02 '19 at 12:30
  • Your currently faulty looking regex is probably meant as [`^(?:[01]\d|2[0-3]):[0-5]\d$`](https://regex101.com/r/2RrBnC/1/) and would match up to `23:59` maybe you just want to make it something like [`^\d{2,3}:[0-5]\d$`](https://regex101.com/r/2RrBnC/2) – bobble bubble Dec 02 '19 at 12:43

2 Answers2

1

I think this should work

^([1-9]{1,2}|[0-9]{1})[0-9]:[0-5][0-9]$
kled
  • 38
  • 5
  • So that would match `012:32`. Why would you want three digits for minutes with the leading digit being `0`? What is the difference between what you have and `^[0-9]{2,3}:[0-5][0-9]$`? – Booboo Dec 02 '19 at 13:48
1

I am working on the assumption that 199:59 is the maximum time you can have. In other words, when the minutes go to three digits, the leading digit must be a 1 (we never want to show a 0). That leads to the following regex:

^1?[0-9][0-9]:[0-5][0-9]$

See Regex Demo

If you want to support up to 399.59, that is when the minutes go to three digits the first digit can be a 1, 2 or 3 (again, we never want to show a 0), then:

^[1-3]?[0-9][0-9]:[0-5][0-9]$

If you want up to 999.59:

^[1-9]?[0-9][0-9]:[0-5][0-9]$
Booboo
  • 38,656
  • 3
  • 37
  • 60