-1

I am trying to retrieve all the phone numbers from a text file using regular expression.

I have written below code for same.

import re
num=input('Enter valid number')
match=re.fullmatch('[6-9+9191]\d{10,13}',num)
    if match!=None:
        print('Mobile number is valid')
    else:
        print('Invalid Mobile number')

I have created pattern to search for numbers starting with 6-9 or +91 or 91 and having length between 9-13. By evaluating above code and if I enter '+919492556' or '987564321' or '919765893146' I'm getting output as valid mobile number,which is not desirable one. I want it to consider different conditions for different mobile number formats in order to correctly validate phone number entered in any format mentioned above. Thanks in advance. My output should be like +91875643 - Invalid mobile number +917465234567 - valid mobile number 917895436271 - Valid mobile number 91945637238 - Invalid mobile number 8765439254719 - Invalid mobile number 8796453271 - Valid mobile number

  • Is `+91` or `91` optional in the start of mobile number? Besides which you want to support numbers starting with 6 to 9 and further nine more digits? It would help if you add some valid and invalid samples in your post. – Pushpesh Kumar Rajwanshi Feb 16 '19 at 17:23
  • @ Toto: OP isn't asking any meaning of regex, so why close a valid question as duplicate to a post which doesn't make any sense? He is seeking a regex that works for validating his mobile number and he has shared his efforts and current code too. – Pushpesh Kumar Rajwanshi Feb 16 '19 at 17:42
  • @PushpeshKumarRajwanshi: When I will see what his regex means, he will certainly found himself the solution. His character class doesn't match what he thinks it does. The linked question will help him. – Toto Feb 16 '19 at 17:58
  • @Toto: Thank you for responding. He seems to know how to use `\d` and ranged quantifier but seems to be confused about how to use character class where a little directed effort may help him. The linked post is great from learning regex but people coming for some specific thing may feel lost seeing the linked post. Perhaps we should give him some pointers and help him achieve his regex. – Pushpesh Kumar Rajwanshi Feb 16 '19 at 18:03
  • @ Maheswar: See if this is what you wanted. [Demo](https://regex101.com/r/MMSJyn/1/) – Pushpesh Kumar Rajwanshi Feb 16 '19 at 19:05
  • @PushpeshKumarRajwanshi By evaluating above code and if I enter '+919492556' or '987564321' or '919765893146' I'm getting output as valid mobile number,which is not desirable one. I want it to consider different conditions for different mobile number formats in order to correctly validate phone number entered in any format mentioned above. Thanks in advance – – Maheswar Reddy Feb 17 '19 at 20:31
  • @Toto I am unable to figure out the regex for my desired output and need some directed help from you guys. I'm new to python and your help will be appreciated. – Maheswar Reddy Feb 17 '19 at 20:39

1 Answers1

0

You can try this pattern for your regex:

pattern = ^[6-9+9191]\w{9,13}

where:

^ : asserts position at start of the string

\w : matches any word character (equal to [a-zA-Z0-9_])

{9,13} : Matches between 9 and 13 times, as many times as possible, giving back as needed

shahriar
  • 362
  • 4
  • 17