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

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()
names_file.close()

# 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)

print(line)
print(line.groupdict())
print(line)
print(line.groupdict())

2 Answers

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,738 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 :)