Phone Number Validater | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Phone Number Validater

Phone number is first digit is 1,9 or8 and total numbers are 8. What I create is the following pattern, but it doesn't work: pattern = r'^[198]{1}[0-9]{7}' I need your help.

27th Apr 2022, 11:37 PM
Taka
Taka - avatar
8 Answers
0
Pls elaborate on "it doesn't work". Which strings give false negatives or false positives? Better if you can do it in the question itself. Pls save your code as public in Code Playground and link it in the question. That said: 1. Why the {1}? 2. You chose correctly to put an anchor in the beginning. What about the ending?
29th Apr 2022, 1:43 PM
Emerson Prado
Emerson Prado - avatar
0
Thank you for your replying. I'm afraid I'm not sure how I can save my code as public. Give me a second. This project is the No.91 of Python course, by the way.
29th Apr 2022, 1:59 PM
Taka
Taka - avatar
0
1. Why the {1}? I had thought {1} means the first number is 1, 9 or8 in this case. 2. You chose correctly to put an anchor in the beginning. What about the ending? What's an anchor? Do you mean {7}? Thank you for your patience. I'm just a beginner. I'm trying again and again everyday.
29th Apr 2022, 2:12 PM
Taka
Taka - avatar
0
In Code Playground, when you save your code, the app asks if you want it to be private or public. If you already saved as private, just click on the word "Private", and you can change. I recommend doing the course on Regular Expressions, also available in SoloLearn. But on the questions: Anchor is a metacharacter of regular expressions indicating the beginning or end of a string. The caret (^) finds the beginning, while dollar sign ($) finds the end. When you include a caret, the expression only matches if it's in the beginning. So you assure the [189] does begin the string. The same works for the dollar sign and the 7 digits. I'll let you finish this thought. The numbers between curly brackets ({1}, {7}) are multipliers. So <Char>{7} means 7 occurrencies - [0-9]{7} matches 7 digits. <Char>{1} matches 1 char, what is already achieved with <Char> itself.
29th Apr 2022, 11:24 PM
Emerson Prado
Emerson Prado - avatar
0
Thanks, Emerson. I've just solved it finally!
25th May 2022, 10:53 PM
Taka
Taka - avatar
0
Taka Great! Would you mind sharing the final expression?
25th May 2022, 11:54 PM
Emerson Prado
Emerson Prado - avatar
0
Like this
30th May 2022, 11:37 PM
Taka
Taka - avatar