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

Sahar Nasiri
Sahar Nasiri
7,454 Points

I have a Traceback error!

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 #Optional Phone
    ([\w\s]+, \s[\w\s.]+)\t?          #Job and Company
    (@[\w\d]+)?$                      #Twitter
''', data, re.X|re.M))

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

It is helpful to include the full traceback error message. The traceback error I get is:

  File "/home/chrisf/.virtualenvs/dj187/lib/python3.4/sre_parse.py", line 512, in _parse
    raise error("bad character range")
sre_constants.error: bad character range

A character range is when you separate two characters within a character class (or set) with a hyphen ("-") as in [a-z]. Any hyphen seen within the character class brackets is assumed to be defining a range unless it is the first character in the class.

The "email" expression has the hyphen between a digit placeholder \d and a period: "\d-.". This is an bad range. Move the hyphen to the first character in the class:

    ([-\w\d.+]+@[-\w\d.]+)\t        #Email