+ 2

Phone Number Validator Test - Python

Hi. I'm trying to solve the quiz but i'm really struggling because the theoric part of Regular Expression in the Python Core course is not very clear. This is how far i went: https://code.sololearn.com/ca11A15A122A Those are the errors i got: Traceback (most recent call last): File "/usercode/file0.py", line 7, in <module> match = re.search(pattern, number) File "/usr/local/lib/python3.8/re.py", line 201, in search return _compile(pattern, flags).search(string) File "/usr/local/lib/python3.8/re.py", line 304, in _compile p = sre_compile.compile(pattern, flags) File "/usr/local/lib/python3.8/sre_compile.py", line 764, in compile p = sre_parse.parse(p, flags) File "/usr/local/lib/python3.8/sre_parse.py", line 948, in parse p = _parse_sub(source, state, flags & SRE_FLAG_VERBOSE, 0) File "/usr/local/lib/python3.8/sre_parse.py", line 443, in _parse_sub itemsappend(_parse(source, state, verbose, nested + 1, File "/usr/local/lib/python3.8/sre_parse.py", line 525, in _parse code = _escape(source, this, state) File "/usr/local/lib/python3.8/sre_parse.py", line 423, in _escape raise source.error("invalid group reference %d" % group, len(escape) - 1) re.error: invalid group reference 8 at position 11

10th Jun 2021, 7:06 AM
Cristopher Ricciotti
Cristopher Ricciotti - avatar
5 Respostas
+ 5
Have a look at this little tweak to your code. import re number = input() #pattern = r"^[1|8|9]\d\8" pattern = r"^(1|8|9)[0-9]+" match = re.search(pattern, number) if match: print("Valid") else: print("Invalid") I am not very good at regex, but I am pretty sure the first part of pattern means that first number must be a 1 or 8 or 9, & the characters follwing the first number must be between 0 & 9 inclusive. Now all you need do is confirm the length of the phone number. Good luck
10th Jun 2021, 7:43 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 3
at 1st part you could either use parenthesis to group digits separated by OR operator or use squared bracket with digits: "(1|8|9)" OR "[189]" (2nd shorter) in 2nd part you have still cheched 1 digit, so you must have 8-1 = 7 digits following... "\d" is correct shorter way, even if you could replace it with character class (range in square bracket): "[0-9]" is strictly equivalent to "\d" "\8" refer the 8th parenthesed capture group... to specify a count for previous char (or group) you must put the number inside curly bracket: "{7}" by using 'search' you must put start and end anchors ("^" and "
quot;), by using 'match' you don't have to use former (implicit pattern checked from start) and by using 'fullmatch' you doesn't need both (implicit full string must match pattern)... 'r' (raw) string prefix used if you want avoid escape: r"\" == "\\" 3 check ways: re.fullmatch(r"[189]\d{7}",number) re.match("(1|8|9)[0-9]{7}",number) re.search("^[189][0-9]{7}
quot;,number) even re.match("[189].{7}
quot;,number) works ^^
10th Jun 2021, 11:43 AM
visph
visph - avatar
+ 2
Cristopher Ricciotti , you can try the match also including the length check: ... pattern = r"^[189][0-9]{7}
quot; # [189] = one of these 3 | [0-9] = digits 0-9, {7} of it | length complete: first digit +{7}=> 8 digits ...
10th Jun 2021, 8:34 AM
Lothar
Lothar - avatar
+ 1
Thank you all Guys, you helped me a lot!
10th Jun 2021, 3:35 PM
Cristopher Ricciotti
Cristopher Ricciotti - avatar
+ 1
Here's a possible solution: import re print("Valid"*bool(re.match(r"[189]\d{7}
quot;, input())) or "Invalid") # Hope this helps
11th Jun 2021, 4:13 PM
Calvin Thomas
Calvin Thomas - avatar