Some of the test cases are not running(phone number validator), why? | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
0

Some of the test cases are not running(phone number validator), why?

import re number = input() pattern=r"(1|8|9)[0-9]+" match=re.match(pattern,number) if len(number)==8: if(match): print("valid") else: print("Invalid")

8th Jul 2022, 6:45 AM
shifa
shifa - avatar
5 Antworten
+ 3
Emerson Prado , if you take moment and check the code that shifa has posted, you can see that there is one issue concerning the length of the phone number. i posted this detail. this is NOT a finished code. additionally i gave i gave a hint about using the findall() function... so i can not agree with your post - sorry!
9th Jul 2022, 8:03 PM
Lothar
Lothar - avatar
+ 2
shifa If len is not 8 then what should print? There is 'Valid' not 'valid'
8th Jul 2022, 7:10 AM
A͢J
A͢J - avatar
+ 2
shifa , we can improve the regex pattern so that it also can match for the length of the complete string: pattern = "^(1|8|9)[0-9]{7}
quot; ^ => match has to start at the beginning of the string $ => match has to end at the end of the string {7} => defines the length of the second character group i would also recommend to use: match = re.findall(...,...)
8th Jul 2022, 12:52 PM
Lothar
Lothar - avatar
+ 1
Lothar I saw this issue. Your explanations on multipliers and anchors are good. I just think you could leave out the finished regexp, for the OP to build it.
9th Jul 2022, 11:59 PM
Emerson Prado
Emerson Prado - avatar
0
Lothar , pls do not give finished code as answer, for the reasons exaustively explained
8th Jul 2022, 2:39 PM
Emerson Prado
Emerson Prado - avatar