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

Sam Dale
Sam Dale
6,136 Points

Can't Solve Blank List Problem

Hi there. No matter what I do I cannot figure out why the following code prints out an empty list. I'm fairly certain that it is all the exact same code as was shown. I'd love it if someone could help me find the obvious mistake that's eluding me. Thanks!

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  # Last and first names
    ([-\w\d.+]+@[-\w\d.]+)\t  # Email
    (\(?\d{3}\)?-?\s?\d{3}-\d{4})\t  # Phone
    ([\w\s]+,\s[\w\s]+)\t  # Job and company
    (@[\w\d]+)  # Twitter 
    """, data, re.X))

The names.txt looks like:

Love, Kenneth   kenneth@teamtreehouse.com   (555) 555-5555  Teacher, Treehouse  @kennethlove
McFarland, Dave dave@teamtreehouse.com  (555) 555-5554  Teacher, Treehouse
Arthur, King    king_arthur@camelot.co.uk   King, Camelot
Österberg, Sven-Erik    governor@norrbotten.co.se   Governor, Norrbotten    @sverik
, Tim   tim@killerrabbit.com    Enchanter, Killer Rabbit Cave
Carson, Ryan    ryan@teamtreehouse.com  (555) 555-5543  CEO, Treehouse  @ryancarson
Doctor, The doctor+companion@tardis.co.uk   Time Lord, Gallifrey
Exampleson, Example me@example.com  555-555-5552    Example, Example Co.    @example
Obama, Barack   president.44@us.gov 555 555-5551    President, United States of America @potus44
Chalkley, Andrew    andrew@teamtreehouse.com    (555) 555-5553  Teacher, Treehouse  @chalkers
Vader, Darth    darth-vader@empire.gov  (555) 555-4444  Sith Lord, Galactic Empire  @darthvader
Fernández de la Vega Sanz, Maria Teresa mtfvs@spain.gov First Deputy Prime Minister, Spanish Govt.
Afrid Mondal
Afrid Mondal
6,255 Points

make the phone optional by adding ? at last and your tab creating the problem maybe. Try this,i think it should help..

import re

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

 print(re.findall(r"""
    ([-\w ]*,\s[-\w ]+)  # Last and first names
    ([-\w\d.+]+@[-\w\d.]+)  # Email
    (\(?\d{3}\)?-?\s?\d{3}-\d{4})? # Phone
   ([\w\s]+,\s[\w\s]+) # Job and company
   (@[\w\d]+)$  # Twitter
    """, data, re.X|re.M))

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

I found these errors:

  • the names.txt data wasn't separated by TABs, just spaces. But that might just be a markdown or cut-and-paste issue
  • need to add ? outside of parens for fields that are optional such as email, phone, twitter
  • missing . for company name
  • the re flag for MULTILINE needs to added: re,X | re.M to parse passed the first line
# fixed version
results = re.findall(r"""
    ([-\w ]*,\s[-\w ]+)\t  # Last and first names
    # add ? for option email
    ([-\w\d.+]+@[-\w\d.]+)?\t  # Email
    # add ? for option phone
    (\(?\d{3}\)?-?\s?\d{3}-\d{4})?\t  # Phone
    # add . for company name
    ([\w\s]+,\s[\w\s.]+)\t  # Job and company
    # add ? for option twitter
    (@[\w\d]+)?  # Twitter
    """, data, re.M | re.X)
Sam Dale
Sam Dale
6,136 Points

Thank you. That helped a lot.