Re module: Python Core | SoloLearn: Learn to code for FREE!

0

Re module: 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

5/13/2021 11:27:46 AM

RockStar

31 Answers

New Answer

+1

what's the problem? just without changing anything in your code, add \ before 1

+2

I think you should show your attempt/code here RockStar See this code 👇 👇 https://code.sololearn.com/crNcBRmWBMf8/?ref=app

+2

Hi! your expression is correct, you just need to use a special sequence character in it

+1

It's still not answering all of the inputs.

+1

Because of the 1,8 or 9. I thought using ^ on character class of [1,8,9] will work for the beginning. But as to how to finish it up. If I use that method, I get all task to be correct with the exception of one, which since I'm not using a pro version, I can't see the input.

+1

Which is? Because I have already used ^ for the beginning.

+1

Yaroslav Vernigora , can you please write a demo for me. And thanks for the clearance of misinformation.

+1

Wow , it worked

+1

import re x = input() pattern = '\A[189]\d{7,7}\Z' if re.match(pattern,x): print('Valid') else: print('Invalid') Even I was stuck at this question, then I realised that it is because the program will match a string of even 12 digits, because I haven't strictly defined the starting and ending points of the string. Thus I added \A in front of string to represent start of string and \Z to represent end of string. In this way if string goes beyond 8 digit, then it would show invalid because it won't match. \d{7,7} basically does the same thing, matches 7 digits, but it doesn't put the condition, that no extra character should be present after these 7 digits. If I don't put \Z at end it will also match strings like '812345670ahdj'.

+1

Oh, \. It is normally used with group, for eg: if I write (778)(62) \1. So basically 778 is one group, and 62 is another group. \1 represents the first index group, that is 778. Similarly \2 represents second group that is 62. So here we can say that (778)(62) \1 is equivalent to writing (778)(62)(778). But one thing to keep in mind is that \n gives exact copy of nth group. For eg: if I write (...)(...) It will accept any three characters in first group and any three characters in second group, basically it will match with any 6 random characters like 123456. But if you write it like this, (...) \1, it doesn't mean (...)(...), Because first group will still accept any three character, but \1 will only accept what group 1 is having. So if you type 123 for group 1, you will have to type same thing in \1 to match it. Eg:123123 or 775775 etc. and \ is also used to escape few special characters inside string like single quote' or double quote" etc. And as you said, it is also used to set template.

+1

Thanks! Exellent answer!

+1

My practice code is not working.? I'm student of python core. print (" hello world!")

+1

pattern = r"^[\1,8,9.......]" I am sorry, but this pattern is absolutely wrong. Square bracket somewhat works like a OR statement, for eg if I write, [12r7] as pattern, then any string with anyone of the value which is present in square bracket will return true. So 1 or 2 or 7 or r or any mixed input like 12 or 7rjdksik will return true. Above, in my example I used [189] because phone numbers first Position can either be 1, 8 or 9. So now it is easy to understand what pattern = r"^[\1,8,9.......]" Is comparing. It will return true is any string have \ or 1 or (comma(,)) or 9 or dot(.) And the best thing is that re.match only matches from beginning and it also doesn't cares about length of string until and unless you limit it with \Z, so even if anyone of these things is present as first character, it will return valid output and if it is not starting with 189, then it is definitely starting with any other number, and for this your output would had been invalid. Thus, randomly some output were correct.

+1

As you said \d is for digits. And {x, y} is for number of repetition of \d. x represents starting point of repetition and y represents end point of repetition. For eg: (7) {2,4} will return true for 77, 777, 7777 or even 7777626. But it will not return true for only one 7, because I have set the range for match as {2, 4} which wants either two, three or four 7. When I wrote \d {7,7}, I set the range from 7 to 7, which means I exactly want seven digits. I did this because out of eight digit first Digit was already decided by [189] and only seven digits were left. After which I ended the string with \Z, so that it doesn't return true for strings of length more than 8.

+1

Here's a possible solution: from re import search as k if k(r"^[189]\d{7}$", input()): print("Valid") else: print("Invalid") # Hope this helps

+1

#Solution without importing a = input() if len(a) == 8: if a[0] in ['8' , '9' , '1']: print("Valid") else : print("Invalid") else : print ("Invalid"(

0

https://code.sololearn.com/cy6a5Gn31Jdd/?ref=app

0

put it before 1. and you are wrong when you think that pro users can see closed tests. no one can see them. they are closed in order to prevent tampering with the results

0

...[\1,8,9...

0

Oooppss, still not up to task.