0

I want to validate my timerlabel which is on the format hh:mm:ss and so far i have done it by this:

if re.match("^[0-9]{2}:[0-9]{2}:[0-9]{2}$", self._lineeditDuration.text()):

The problem with the above line is that the user can enter 00:99:99, which does not make sense that the minutes and seconds can be higher than 59.

Is there a way to validate each of the numbers that represents minutes, so the first number can be 0-5 and the second number can be 0-9. The same goes for seconds.

smci
  • 32,567
  • 20
  • 113
  • 146
sergejacub
  • 11
  • 7

2 Answers2

0

this should work if re.match("^[0-5][0-9]:[0-5][0-9]:[0-5][0-9]$", self._lineeditDuration.text()):

Valentin C
  • 176
  • 7
  • Except this also allows hh to be 00..59, which is wrong. hh is either in the range 00..23? 01..12? with or without leading zero – smci Jun 25 '21 at 08:36
  • Since this is not a time but a timer label I think any limitation or even no limitation on hh would be OK since a 99:59:59 timer would be totally valid. – Niko B Jun 25 '21 at 09:04
  • if re.match("^[0-9]{2}:[0-5]{1}[0-9]{1}:[0-5]{1}[0-9]{1}$", self._lineeditDuration.text()): this works – sergejacub Jun 25 '21 at 10:39
0

00:00:00
00:00:01
23:59:17
01:01:01
10:17:25
09:56:17
22:56:59

Find and Match : ^([0-1]?\d|2[0-3])(?::([0-5]?\d))?(?::([0-5]?\d))$
view Test https://regex101.com/r/qokOxD/1

  • This regex is not satisfying the constraints that OP has provided. "hh has no limits, just minutes and seconds" yet this regex fails for 99:00:00 – Flair Nov 27 '21 at 04:25
  • correction ^([0-1]?\d|2[0-3])(?::([0-5]?\d))?(?::([0-5]?\d))$ https://regex101.com/r/i4KK1D/1 – Gouraya4912 Nov 27 '21 at 13:24