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 Review: Regular Expressions in Python

Elizabeth McInerney
Elizabeth McInerney
3,175 Points

I am stuck on getting the contents of a group named email from my match object. I tried match_object.groupdict(email)

No more details, I just can't figure this one out. I don't recall this from the group video.

4 Answers

Elizabeth McInerney
Elizabeth McInerney
3,175 Points

I just tried this and it failed, but I could not read why because it still gave me credit for the quiz and the big box announcing my credit obscured the reason. Just FYI

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Ah, I see the question you're talking about.

This doesn't require groupdict() because we don't want a dict of the groups, we want just the content of a single group. We use the .group() method and pass it the name of the group we want. So the answer is group('email') (since the . is already in the prompt).

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

.groupdict() gives you back a dict. To get the group (key) from it, you'd need to access the key of the returned dict, so .groupdict()['email'].

Neil Gordon
Neil Gordon
8,823 Points

thank youKenneth Love for the explanation

match_obj.group('group_name')

Elizabeth McInerney
Elizabeth McInerney
3,175 Points

Thanks, that worked. I guess I just don't remember the group() method. Was it used recently?

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

We went over the group() method, yes. We didn't use it a lot because, usually, the groupdict() is more useful. Sometimes, though, my quizzes and code challenges are going to require you to look things up in the documentation or experiment in your code or the Python shell to find the right answer. Research is probably the most useful tool a programmer has.