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.

12th Jun 2021, 8:40 AM
Ritesh Behera
Ritesh Behera - avatar
6 Respostas
+ 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;
12th Jun 2021, 11:44 AM
visph
visph - avatar
+ 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 ^^
12th Jun 2021, 12:01 PM
visph
visph - avatar
+ 1
Idk, but it works
12th Jun 2021, 11:56 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 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
12th Jun 2021, 12:46 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
0
Hi! 189 enclose in square brackets and before 1 add \
12th Jun 2021, 8:57 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
0
Yaroslav Vernigora why want you put \ before 1?
12th Jun 2021, 11:47 AM
visph
visph - avatar