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 Groups

Jonathan Mitten
PLUS
Jonathan Mitten
Courses Plus Student 11,197 Points

Following in console, I get no results

I'm starting the video over from the beginning, because I figured I'd missed something. However, in the console (macOS, python 3.5.4), I get an empty list instead of any results with this code:

import re

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

print(re.findall(r'''
    ([-\w ]+,\s[-\w ]+)\t
    ([-\w\d.+]+@[-\w\d.]+)\t
    (\(?\d{3}\)?-?\s?\d{3}-\d{4})\t
    ([\w\s]+,\s[\w\s]+)\t
    (@[\w\d]+)
    ''', data, re.X))

According to the video, this is a "good start" that will omit some of the expected results, but I'm getting nothing.

Going back to older patterns from previous, I am getting results, so I'm confident it's not a file issue.

Can anybody spot my error?

2 Answers

Jonathan Mitten
PLUS
Jonathan Mitten
Courses Plus Student 11,197 Points

Actually, I think I've solved this and others' issues with us using our console. I suspect the issue is in copying and pasting the names.txt file into a text editor that replaces tabs with spaces. My set up for editing Python in Sublime Text 3 swaps tabs with 4x spaces, rendering some of the regex rules invalid.

Gabbie Metheny
Gabbie Metheny
33,778 Points

I did this course in workspaces, not a Mac console, but at first glance, it looks like you need a MULTILINE flag in addition to your VERBOSE flag, since you want the regex to search one line of data at a time and treat new lines as new strings to evaluate. Does that produce the desired results?

Jonathan Mitten
Jonathan Mitten
Courses Plus Student 11,197 Points

I'm not quite to that stage yet... I was testing just the preliminary regex, which show results in the video, just not the full result-set that's shown later in the video.

Gabbie Metheny
Gabbie Metheny
33,778 Points

Gotcha. Sounds like you figured out what was going wrong!