0

Stuck with the regex quiz in Python

The code passes all five tests except the third one which i Can not see since i'm not a pro membre. I couldn't find the error. #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 import re number = str(input()).replace(" ", "") number = number.strip() pattern = "^([189])(\d){7}" match = re.match( pattern , number ) if match : print("Valid") else : print("Invalid")

12th Apr 2021, 5:07 PM
Soufiane Drissi
3 Respostas
+ 1
One advice==> Don't make simple problems complex as the chances of getting an error increases. Question asks us to write logic for 2 conditions. 1. It should start with 1|8|9 2. It should have exactly 8 numbers. Excluding the first num, it will have 7. Write a regex pattern satisfying these two conditions. pattern = r"^[189]\d{7}
quot; You are done. remove that replace and strip method, they are unessential here. Simply add one $ at the end to specify the end point
12th Apr 2021, 5:25 PM
CHANDAN ROY
CHANDAN ROY - avatar
+ 1
Thanks for your answers. I forfait the $ metacharacter , so my pattern allowed for more than 8 digits.
12th Apr 2021, 5:30 PM
Soufiane Drissi
0
Hint: In your code 123456789 or 9876543210-1-2-3+4 was valid too, it's wrong.
12th Apr 2021, 5:10 PM
Илья Мирошник
Илья Мирошник - avatar