Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Regular Expressions in Python Introduction to Regular Expressions Phone Numbers

How can this challenge tell me my solution is correct while from the console i got empty list []

Challenge Task 1 of 1 Create a function named phone_numbers that takes a string and returns all of the phone numbers in the string using re.findall(). The phone numbers will all be in the format 555-555-5555.

Here is my solution for the code challenge which i got correct.

def phone_numbers(string):
    return re.findall(r'\d{3}-\d{3}-\d{4}', string)

At the below code, i was trying to test it with my local number, but after testing i got an <empty list []> instead of <0801-234-5678>

import re


def phone_numbers(string):
    return re.findall(r'\d{4}-\d{3}-\d{4}', string)

mobile_num = input("Enter your phone number eg,(08012345678): ")

if len(mobile_num) > 11:
    print("Mobile number longer than 11...")
elif len(mobile_num) < 11:
    print("Mobile number lesser than 11...")
phone_num = phone_numbers(mobile_num)
print(phone_num)

i tested my modified app to test a local number... finally this what i got. can someone please check and correct my code

-----------------------treehouse console---------------------------

treehouse:~/workspace$ python exercises.py
Enter your phone number eg,(08012345678): 08062134747
treehouse:~/workspace$ python exercises.py
Enter your phone number eg,(08012345678): 08062134747
[]

1 Answer

John Lack-Wilson
John Lack-Wilson
8,181 Points

You are getting an empty list because the number you are providing to the regular expression, 08062134747, doesn't match the regular expression format.

The regular expression you have will only find numbers that are in the format: 123-123-1234. I believe you are wanting the regular expression to take your number and turn it into the format which includes dashes, but that isn't what regular expressions are used for.