+ 14

[SOLVED] Python Phone Number Validator

Hi! This is the only challenge I need to do, to finish the python course. The valid number starts with 1or8or9 and has the length of 8. My code passed the first 4 tests, but it failed the 5th test. I can't see the phone number in the 5th test. What can be the problem? Current code: import re number = input() if re.search("([189])[\d]+", number) and len(number) == 8: print("Valid") else: print("Invalid")

13th Nov 2020, 12:54 PM
Csongor Kertesi
Csongor Kertesi - avatar
47 Answers
+ 4
https://code.sololearn.com/cA3A20a15A2a You can find answer in the above link.
19th Feb 2021, 3:50 PM
Basupalli Shiva Kumar Reddy
Basupalli Shiva Kumar Reddy - avatar
+ 54
import re pattern = r"^[189][0-9]{7}
quot; if re.match(pattern, input()): print("Valid") else: print("Invalid") =========================================== I hope this helps you all. ^ is for starts with [189], [0-9] digits for {7} repetiotions $ to end total of [189] + [0-9]{7} which is equals to 8. Hope you have understood my answer😊
21st May 2021, 8:03 AM
Pratik shinde
+ 8
import re a = input() pattern = r"[189]" if re.match(pattern,a): if len(a) == 8: print("Valid") else: print("Invalid") else: print("Invalid")
13th May 2021, 7:46 AM
SATHIYA PRIYA IYAPPAN
+ 5
import re #your code goes here number = input() if re.match(r"^[1|8|9]\d{7}
quot;, number): print("Valid") else: print("Invalid")
1st Jun 2021, 6:27 AM
Krystian Jabłonowski
Krystian Jabłonowski - avatar
+ 4
import re pattern = r"^[189][0-9]{7}" str = input() match = re.search(pattern, str) if match: print("Valid") else: print("Invalid") What wrong with this code?
8th Feb 2021, 3:50 AM
Arya Huda Arrasyid
Arya Huda Arrasyid - avatar
+ 4
i still get one case not correct this is my code where is the mistake ? import re #your code goes here pattern = r"^1|8|9 + [0-9]{7}
quot; num = str(input()) if re.search(pattern, num): print("Valid") else: print("Invalid")
25th Feb 2021, 12:57 PM
Smile Sam
Smile Sam - avatar
+ 4
Here is my answer to that guys: import re number = input() pattern = r"^[189][0-9]" match = re.match(pattern, number) if len(number) == 8 and match: print("Valid") else: print("Invalid")
12th Apr 2021, 12:06 AM
Karl Joseph Buenafe
Karl Joseph Buenafe - avatar
+ 4
@Arya Huda Arrasyid You need a $ at the end of your regex, otherwise, the phone number can start with the correct 8 digits but then go on indefinitely. It can only be 8 digits
24th Jul 2021, 4:40 AM
Stephanie Chua
Stephanie Chua - avatar
+ 3
Most probably you're getting this failure in last test case cause re.search() searches for the whole string even if the string contains multi-lines and tries to find a match of the substring in all the lines of string but you want your numeric number to only START with 1,8 or 9. better to use re.match() instead
6th Mar 2021, 7:05 AM
Prakhar
Prakhar - avatar
+ 3
import re num = input () pattern = r"^[1|8|9][0-9]*
quot; if re.match(pattern,num) and len(num)==8: print("Valid") else: print("Invalid")
8th Mar 2021, 6:19 PM
hassan outaja
hassan outaja - avatar
+ 3
I think this is simplest: import re phone = input() pattern = r"^[189][0-9]{7}
quot; if re.match(pattern, phone): print("Valid") else: print("Invalid")
6th Apr 2021, 4:33 PM
Lukasz Oltarzewski
Lukasz Oltarzewski - avatar
+ 3
import re #your code goes here number = input() pattern = r"^(1|8|9)" match = re.match(pattern, number) if match and len(number) == 8: print("Valid") else: print("Invalid")
15th May 2021, 2:34 PM
Akhil Kaushik
Akhil Kaushik - avatar
+ 2
I recommend to test the code on the SL Playground with a few data inputs and a few print statements and see what happend and solve that. Sp you could see that 01234567 gives a valid number, what unfortunately is not true from task description. The solution for it will be to code that the number has to begin with 1 or 8 or 9.
13th Nov 2020, 1:55 PM
JaScript
JaScript - avatar
+ 2
import re #tu código va aquí pattern = "\d\1|8|9{1,9}
quot; number = input() if re.search(pattern, number) and len(number) == 8: print("Valid") else: print("Invalid")
20th May 2021, 5:02 AM
yonis Alvarez
yonis Alvarez - avatar
+ 2
Try this: import re #your code goes here str = input() pattern = r"^(1|8|9)+\d{7,7}
quot; if re.match(pattern , str): print("Valid") else: print("Invalid")
28th Jul 2021, 9:43 PM
★××kigurupeter××
★××kigurupeter×× - avatar
+ 2
Okime Obia It's the python library for regular expressions
11th Nov 2021, 2:50 PM
Csongor Kertesi
Csongor Kertesi - avatar
+ 1
import re print("Valid" if re.match(r'[789]\d{7}
#x27;, input()) else 'Invalid')
22nd Jun 2021, 2:19 PM
Shivani Sharma
Shivani Sharma - avatar
+ 1
Mi respuesta es: import re pattern = r"(1|8|9)[0-9]{7}
quot; entrada = input() if re.match(pattern, entrada): print("Valid") else: print("Invalid")
15th Nov 2021, 10:18 PM
Julissa Bedor Espinoza
Julissa Bedor Espinoza - avatar
+ 1
import re num = input () pattern = r"^[1|8|9][0-9]*
quot; if re.match(pattern,num) and len(num)==8: print("Valid") else: print("Invalid")
19th Nov 2021, 3:06 PM
Stefan Bartl
Stefan Bartl - avatar
+ 1
import re x=input() if re.fullmatch("[189]\d{7}",x): print('Valid') else: print("Invalid")
26th Dec 2021, 8:23 AM
Ayu Lestari Gunawan