- 1

Help required: Check valid number

There's a bug in it. Kindly find it out! import re #your code goes here num = input() pattern = r"[189][0-9]{7}" if re.match(pattern, num): print("Valid") else: print("Invalid")

16th Mar 2021, 2:22 AM
Nasir Niamat
Nasir Niamat - avatar
3 Antworten
+ 3
You just need to signify the beginning and end of the match in the pattern. r"^[189][0-9]{7}
quot; Note you can also use [\d] in place of [0-9] it is the shorthand way.
16th Mar 2021, 2:45 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
with 'match' method, you can avoid the starting anchor (^), as 'match' only search from start of string (unlike 'search' method, wich match from everywhere in the string)... however, end anchor ($) is required to be sure there's nothing after the 8th digit ;)
16th Mar 2021, 6:54 AM
visph
visph - avatar
+ 1
Thanks visph, it worked.
16th Mar 2021, 7:37 AM
Nasir Niamat
Nasir Niamat - avatar