+ 1
Help please regular expression python core
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 #your code goes here pattern = r"^[189]\d{7}" txt = input() match = re.match(pattern,txt) if match: print ("Valid") else: print ("Invalid")
6 Antworten
+ 2
#your code is correct but you only need to add input length condition which should be restricted to be 8 digits only
import re
pattern = r"^[189]\d{7}"
txt = input()
match = re.match(pattern,txt)
if match and len(txt)==8: # added condition
print ("Valid")
else:
print ("Invalid")
+ 1
Thank you it works 😁
0
pattern = r"(?=^.{8,8}$)(^[189]\d{7})"
0
Vasiliy re.match only check the existence of 8 digits but doesn't restrict input length so we add another condition to restrict input length. If you tried it out by yourself you would knew it, unfortunately you didn't 🙄 here'd a golden advice for coding and for entire life: if you wanna avoid embarrassing situations, don't talk about smthg you never tried!
0
iTech , agree - wrong.
But why react so harshly?
Only the one who does nothing is not mistaken.
Or do you consider yourself an ideal? 😂
In any case, this can be done with one template (which I indicated in my answer), and if you knew this, you would not add additional conditions. 😂
0
Vasiliy ok,myself and SoloLearn team who made all questions and the answers are wrong and you are the only super hero!
here we go again, another stupid who insists on his/her stupidity and keep arguing!