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 Counts

Mary Yang
Mary Yang
1,769 Points

Python Regular Expressions - Counts, Code not working.

Hi,

I'm watching the Python video covering counts in Python regular expressions and coding along with the video in workspaces. I've quadruple checked my code and even had someone else review it to see if they can find any differences between my code and Kenneth Love's code, and we can't find any differences.

The issue I'm having is that my second search is returning what it's supposed to, but my first search is just returning a blank list. If I change re.findall, to re.match, or re.search it just returns 'none'.

Can someone look over the code and tell me where I've made a mistake please?

import re

names_file = open("names.txt", encoding="utf-8")
data = names_file.read()
names_file.close()


print(re.findall(r'\(?\d{3}\)?-?\s\\d{3}-\d{4}', data))
print(re.match('\w+, \w+', data))

Edit: I should maybe add that the first search is supposed to find all phone numbers in the text document, and the second search is returning a first and last name on the first line only. Thanks!

2 Answers

Mikael Enarsson
Mikael Enarsson
7,056 Points

Ok, I figured out. First, a comparison:

print(re.findall(r'\(?\d{3}\)?-?\s\\d{3}-\d{4}', data))   #Your code
print(re.findall(r'\(?\d{3}\)?-?\s?\d{3}-\d{4}', data))   #My code

Maybe the differences are obvious, but I'll mention them anyway:

  1. After \s, you have an extra backslash, escaping the next backslash and searching for a string with a backslash in it. Fixing this makes .findall() return some of the numbers.

  2. The reason it only returns some of the numbers is that, in it's current form, the string must have a space between the area code and the main number. You need to add a question mark before the \s as well.

That should fix it ^^

Mary Yang
Mary Yang
1,769 Points

Gaaah, I don't know how I missed that like 20 times! lol, even the person I recruited to help me see what I was missing missed it.

Thank you very much for taking the time to look that over and respond. It's working now.

Mikael Enarsson
Mikael Enarsson
7,056 Points

No problem ^^ It was really hard to catch. I had to try to break the string into smaller pieces and find the piece that didn't work XD

John Mercer
John Mercer
31,479 Points

A simpler solution is:

re.findall(r'\d{3}-\d{3}-\d{4}', data)

In any case, it worked for me.