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

Stephen Cole
Stephen Cole
Courses Plus Student 13,822 Points

Can I cherry pick patterns within a string using Regular Expressions?

When using re, in the exercises (in Groups, for example), I have to use include a comma and a space between the two items I want. (Email and Phone.)

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.match(r'''
''', string, re.M|re.X)

If I remove the comma and the space (,\s) the re won't work? Why not? The patterns, to themselves, seem to be unchanged.

1 Answer

Chris Freeman
Chris Freeman
Treehouse Moderator 68,029 Points

Keep in mind that the entire regex pattern must match the target string or nothing gets returned. While the groupings help formulate the results, the comma-space is needed to properly parse the input.

Without the comma-space, The pattern would not match unless there was an email address followed by a phone number with no comma space in between.