-1

I am trying to validate the time format. Below is my code.

x = re.search('([0-9]{1,2}):([0-9]{2})','7:30')
x.string
'7:30' (which is true)

But

x = re.search('([0-9]{1,2}):([0-9]{2})','700:30')
x.string
'700:30'

Should result in nonetype but showing the value)

Wiktor Stribiżew
  • 561,645
  • 34
  • 376
  • 476
Raghavendra
  • 532
  • 4
  • 9

1 Answers1

-2

The second example is matching the text 00:30. You should rather use the function re.match.

x = re.match('([0-9]{1,2}):([0-9]{2})','7:30')
print(x)

x = re.match('([0-9]{1,2}):([0-9]{2})','700:30')
print(x)

output:

<re.Match object; span=(0, 4), match='7:30'>
None
pyOliv
  • 1,118
  • 5
  • 20