-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
  • 607,720
  • 39
  • 448
  • 563
Raghavendra
  • 521
  • 5
  • 11

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,253
  • 1
  • 6
  • 21