0
Please help me to find out what's wrong here in this code for the phone number validator challenge.
import re #your code goes here number = str(input()) pattern = r"^1|8|9+.{6,7}" if re.match(pattern , number) : print("Valid") else : print("Invalid") I am getting all the test cases right except one. 🙏🙏 help.
6 odpowiedzi
+ 2
1) you doesn't need to convert input() to string as it's already a string
2) you use match() method, so implicitly only search from start of string
3) your pattern is wrong: actually it must be read as "from start: (find a '1') OR (find a '8') OR (find one or more '9' followed by at least 6 to 7 chars)...
instead of start anchor you must specify end anchor or use fullmatch() method
your OR for first char must be enclosed in parenthesis, or better enclosed in square bracket without the OR operators (define character class)
the end of your pattern must match any digit 7 times no more, no less (as first digit already checked, to get 8 digits to be valid): use \d or [0-9] followed by {7}
so, your match pattern could look like: r"[189]\d{7}quot;
+ 2
yes, if you put it in the square bracket it should work for test cases but the regex would return valid for invalid string as accept 7 digits numbers following a starting \ (assuming rest of regex is fine)... you can also pass tests if you only check for [189] followed by 7 any chars (no more), but the goal is to output valid only for numbers ^^
+ 1
Idk, but it works
+ 1
your explanations are very valuable for me, I will save them to understand in the future, because I have not yet got to the topic of regular expressions in my python studies
0
Hi! 189 enclose in square brackets and before 1 add \
0
Yaroslav Vernigora why want you put \ before 1?