- 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")
3 odpowiedzi
+ 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.
+ 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 ;)
+ 1
Thanks visph, it worked.