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 Compiling and Loops

Arguments for MatchObject.groupdict() and MatchObject.group()

When you use MatchObject.groupdict() are you allowed to pass any arguments into it, for example if you want to get the contents of a specific group can you say MatchObject.groupdict("group name") or are you supposed to use MatchObject.group()? The reason I'm asking this is because of the question on the final quiz: "How would I get the contents of the group named email from my match object?" This was the code they gave me to fill in the blank "match_object.(put answer here)"

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

Referring to the MatchObjects docs, you can:

# refer to group number; first match
match_obj.group(0)

# refer to group name; named match
match_obj.group(β€˜email’)

# refer to tuple of matches; first match
match_obj.groups()[0]

# refer to dict of named matches
match_obj.groupdict()[β€˜group_name’]

Post back if you need more help. Good luck!!!