+ 1

can someone help me in python3 re module (phone number validation)?

i have a problem with only one case in the phone number validation and it's make me little nervous can some one explain to me how i pass that and where is the problem in my code? mission: ``` Phone Number Validator 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_from_user = input() pattern = r"\A(1|8|9)\d{7}" res = re.search(pattern, number_from_user) if res: print("Valid") else: print("Invalid") ```

26th Apr 2021, 1:59 AM
Imrane Aabbou
Imrane Aabbou - avatar
3 Respostas
+ 4
Your pattern is incorrect. You don't need the \A 189 should be in a single class [ ], not an ORed group () \d{7} this part is correct Now you just need to add the 'match end' metacharacter.
26th Apr 2021, 2:20 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
@ChaoticDawg ohhhhhh i was so stupid bro thank you so much for your help !
26th Apr 2021, 4:14 AM
Imrane Aabbou
Imrane Aabbou - avatar
0
import re #your code goes here num=input() pat=r"^1|8|9" if len(num)==8: if re.match(pat,num): print("Valid") else: print("Invalid") else: print("Invalid")
11th Nov 2021, 3:34 PM
Aniruddha Ganguly
Aniruddha Ganguly - avatar