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

Android

Introduction to Regular Expressions Phone Numbers Challenge

http://teamtreehouse.com/library/regular-expressions-in-python/introduction-to-regular-expressions/phone-numbers

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.

Sounds like a simple challenge (if you are an expert at regex..I'm not)

I like that Kenneth Love said (away down in this long forum thread): https://teamtreehouse.com/forum/python-regular-expressions-display-all-numbers-except-567

"Absolutely nothing in the history of programming is responsible for more broiled computers than regular expressions." (and he has a nice video clip of himself to go along with this quote --check it out!)

So, looking at the video (and I always try to go along with the video): http://teamtreehouse.com/library/regular-expressions-in-python/introduction-to-regular-expressions/counts

..there's a line typed in the video that goes something like:

print(re.findall(r'\(?\d(3)\)?-?\s?\d(3)-d(4)', data))

Great!

I 'translated' (refactored) that into a function like so:

import re
 def phone_numbers(str1):
    return re.findall(r'\(?\d(3)\)?-?\s?\d(3)-d(4)', str1)

It didn't pass (of course)

You see the issue, though, right?

The challenge is looking for : 555-555-5555

It's not looking for (as in the video): (555)555-5555

So I tried different variations

and got different Bummer! errors:

+Bummer! IndentationError

+Bummer! SyntaxError

+Bummer! Didn't get the right number of phone numbers.

Oh, here's an interesting one.

This code snippet:

import re

def phone_numbers(str1):
    return (re.findall(r'?\d(3)?-?\s?\d(3)-d(4)', str1))

..produced an error that said:

Bummer! nothing to repeat

So..

Does it mean you are making progress if the Bummer! errors you get are changing?

Anyway, there probably is some way to pass this challenge using d(3) and d(4),

but I gave up and resorted to doing the 'dumb' way:

import re

def phone_numbers(str1):
  return re.findall(r'\d\d\d-\d\d\d-\d\d\d\d', str1)

Special note from Ken in this forum thread/post: https://teamtreehouse.com/forum/regular-python-expressions-challenge-task-2-of-2-please-clarify-the-question-for-me

"Well, /d/d/d/d/d is not the same thing as \d\d\d\d\d. The first one will be looking for exactly five / with five ds in between them. The second will match five numbers in a row."

..and just for you Python docs reference nuts (you know who you are),

..here's the Python Regex findall() link that I found in another forum thread (didn't help me at all, though): https://docs.python.org/3.4/library/re.html#re.findall

On to the Count Challenge (that from all the forum posts looks like the one that gives people real issues..)

5 Answers

Here you are:

import re

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

This is what i did. It appears the rest of these entries are overthinking the question.

are the '?' necessary? since the question says in what format the phone number would be?

import re

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

i wouldn't say you are making progress, its just you are introducing a different error.

btw r'?\d(3)?-?\s?\d(3)-d(4)' you are forgetting a \ on the final d & leaving in ? at the start.. but fixing that still didn't work for me until i tried \d\d\d... weird.. but I have just realized what the problem was:

import re

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

use {3} instead of (3)...

Why wouldn't this work? I successfully did it in IDLE: '\d*-\d*-\d*'

import re

phone_numbers(strl): return re.findall('\d\d\d-\d\d\d-\d\d\d\d', strl)