Amit Dhamankar
Amit Dhamankar

Reputation: 3

Finding valid phone numbers in text, no numbers found

I am writing a program which checks a string to ensure whether it is a valid phone number(such as 415-555-1011)

def isPhoneNumber(text):
    if len(text)!= 12:
        return False
    for i in range(0,3):
        if not text[i].isdecimal():
            return False
        if text[3]!='-':
            return False
    for i in range(4,7):
        if text[i].isdecimal():
            return False
        if text[7]!='-':
            return False
    for i in range(8,12):
        if not text[i].decimal():
            return False
        return True

message = 'Call me at 415-555-1011 tomorrow. 415-555-9999 is my office.'
for i in range(len(message)):
    chunk = message[i:i+12]
    if isPhoneNumber(chunk):
        print('Phone number found: ' + chunk)

print('Done')

I should get the following output

Phone number found: 415-555-1011
Phone number found: 415-555-9999
Done 

But I am getting only

Done

I am not getting the first two lines

Upvotes: 0

Views: 211

Answers (2)

shubham
shubham

Reputation: 182

Hi You can try this logic:-

def msg_phone():
    message = 'Call me at 415-555-1011 tomorrow. 415-555-9999 is my office.'
    number=re.findall('[0-9]{3}-[0-9]{3}-[0-9]{4}',message)
    for i in number:
        print('Phone number found:',i)
    print('Done')

The code length is lot less,and so is the complication.

Upvotes: 0

Martijn Pieters
Martijn Pieters

Reputation: 1123400

You are missing a not in your second loop:

for i in range(4,7):
    if text[i].isdecimal():
        return False

In your last loop you use text[i].decimal() instead of text[i].isdecimal().

This returns False when any of those characters are digits. You wanted to use:

for i in range(4,7):
    if not text[i].isdecimal():
        return False

You also test for a dash character at the next position for each iteration in each of those number loops (you only need to test after those loops), and your return True at the end needs to be moved out of that loop too (you only test for a digit at position 8).

You don't need to test each individual character; you could just use a slice:

def isPhoneNumber(text):
    if len(text) != 12:
        return False
    if not text[:3].isdecimal():
        return False
    if text[3] != '-':
        return False
    if not text[4:7].isdecimal():
        return False
    if text[7] != '-':
        return False
    if not text[8:12].isdecimal():
        return False
    return True

You could simplify all this by using a regular expression:

import re

phone_numbers = re.compile(r'\b\d{3}-\d{3}-\d{4}\b')

for number in phone_numbers.findall(message):
    print('Phone number found:', number)

Note that this pattern actually prohibits the phone number to be part of a longer set of numbers (your code would find phone numbers in 0014213012345-444-123244214324; all that is required is a middle group of 3 digits surrounded by dashes and a 3 digits prefix and 4 digit postfix). The \b pattern requires there to be non-word characters (anything but letters, digits or underscores) before and after the matched phone number.

Upvotes: 1

Related Questions