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

Sahar Nasiri
Sahar Nasiri
7,454 Points

I have a Traceback error!

    ^([-\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
Chris Freeman
Treehouse Moderator 68,029 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