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 Email Groups

Shawndee Boyd
Shawndee Boyd
5,917 Points

Make a new variable, twitters

I am confuse as to what I have wrong. Please help.

emails.py
import re

string = '''Love, Kenneth, kenneth+challenge@teamtreehouse.com, 555-555-5555, @kennethlove
Chalkley, Andrew, andrew@teamtreehouse.co.uk, 555-555-5556, @chalkers
McFarland, Dave, dave.mcfarland@teamtreehouse.com, 555-555-5557, @davemcfarland
Kesten, Joy, joy@teamtreehouse.com, 555-555-5558, @joykesten'''

contacts = re.search(r'''
    (?P<email>[-\w\d.+]+@[-\w\d.]+)
    ,\s
    (?P<phone>\d{3}-\d{3}-\d{4})
''', string, re.X|re.M)
print(contacts)

twitters = (re.search(r'@[\w\d]+)$', contacts, re.M))

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,167 Points

When I run your code in ipython I get:

twitters = (re.search(r'@[\w\d]+)$', contacts, re.M))
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-29-713b44e409ce> in <module>()
----> 1 twitters = (re.search(r'@[\w\d]+)$', contacts, re.M))

/usr/lib/python2.7/re.pyc in search(pattern, string, flags)
    140     """Scan through string looking for a match to the pattern, returning
    141     a match object, or None if no match was found."""
--> 142     return _compile(pattern, flags).search(string)
    143 
    144 def sub(pattern, repl, string, count=0, flags=0):

/usr/lib/python2.7/re.pyc in _compile(*key)
    242         p = sre_compile.compile(pattern, flags)
    243     except error, v:
--> 244         raise error, v # invalid expression
    245     if not bypass_cache:
    246         if len(_cache) >= _MAXCACHE:

error: unbalanced parenthesis

There are some missed placed parens In the line:

twitters = (re.search(r'@[\w\d]+)$', contacts, re.M))
  1. The parens wrapping the whole statement are unnecessary and should be removed.
  2. Add missing open paren to group the "twitter" group. Open "(" between r' and @. The close ")" appears to be in the correct place.

Try this:

twitters = re.search(r'(@[\w\d]+)$', string, re.M)
Shawndee Boyd
Shawndee Boyd
5,917 Points

Thank you, Chris! I was just thinking too fast.