There is a problem in one hidden case , Can you help me ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

There is a problem in one hidden case , Can you help me ?

#You are given a number input, and need to check if it is a valid phone number. #A valid phone number has exactly 8 digits and starts with 1, 8 or 9. #Output "Valid" if the number is valid and "Invalid", if it is not. #Sample Input #81239870 #Sample Output #Valid #My code import re number = input() pattern1 = r"1|8|9\d{7}" match1 = re.match(pattern1, number) if match1: print ("Valid") else : print("Invalid") >>>>Where is the problem ?

26th Dec 2022, 1:35 PM
Ben Hamida Mohamed Amine
Ben Hamida Mohamed Amine - avatar
5 Answers
+ 4
Try r"[1|8|9]\d{7}
quot;
26th Dec 2022, 1:59 PM
Jayakrishna 🇮🇳
+ 6
re.match is trying to find the regex from the beginning of the string. But doesn't check automatically if the string has any other characters left. You can insert a dollar sign in the regex to mark the end of string. r"1|8|9\d{7}
quot;
26th Dec 2022, 1:44 PM
Tibor Santa
Tibor Santa - avatar
+ 3
Also, That regex checks number starts with 1 or 8 or 9 followed by 7 digits. So it may have any digits next, not restrict to length 8. 1, 12, 123478978989 are also valid but should be evaluated to invalid.
26th Dec 2022, 1:56 PM
Jayakrishna 🇮🇳
0
Yes i did that but nothing changed
26th Dec 2022, 1:50 PM
Ben Hamida Mohamed Amine
Ben Hamida Mohamed Amine - avatar
0
I changed this and and added $ but the problem still existing
26th Dec 2022, 1:58 PM
Ben Hamida Mohamed Amine
Ben Hamida Mohamed Amine - avatar