Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Regular Expressions in Python Introduction to Regular Expressions Groups

Gilang Ilhami
Gilang Ilhami
12,045 Points

Regular Expression - Groups

Couldn't really find what's wrong with the code It returns an error

import re

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

# last_name = r'Love'
# first_name = r'Kenneth'
# print (re.match(last_name, data))
# print (re.search(first_name, data))
# print (re.findall(r'\(?\d{3}\)?-?\s? \d{3}-\d{4}', data))
# print (re.findall(r'\w*, \w+', data))
# print(re.findall(r'[-\w\d+.]+@[-\w\d.]+', data))
# print(re.findall(r'\b[trehous]{9}\b', data, re.IGNORECASE))
# print(re.findall(r'''
#     \b@[-\w\d.]* # First a word boundry, an @, and then any number of characters
#     [^gov\t]+ # Ignore +1 instances of the letter 'g', 'o', or 'v' and a tab
#     \b # Match another word boundry
# ''', data, re.VERBOSE|re.I))

# print(re.findall(r"""
#     \b[-\w]*, #Find a wordboundary, 1+ hypens or characters, and comma
#     \s # Find 1 whitespace
#     [-\w ]+ # Find hypens, characters, and explict characters
#     [^\t\n] # Ignore tabs and newlines
# """, data , re.X))
line = re.findall(r'''
    ^(?P<name>[-\w ]*,\s[-\w ]+)\t # Last and first name 
    (?<email>[-\w\d.+]+@[-\w\d.]+)\t # Email
    (?<phone>\(?\d{3}\)?-?\s?\d{3}-\d{4})?\t # Phone
    (?<job[\w\s]+,\s[\w\s.]+)\t? # Job and Compnay
    (@[\w\d]+)?$ # Twitter
''', data, re.X|re.MULTILINE)


2 Answers

Brendan Whiting
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,678 Points

Here's what I can gather:

  • you need to put in the P in the named groups: (?P<name>...)
  • you're missing the closing > in the job group
  • you need to use the search method instead of findall
Gilang Ilhami
Gilang Ilhami
12,045 Points

Thank you very much brendan :)