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

Laurens Sandt
Laurens Sandt
3,513 Points

regex grouping question

consider my code:

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

am I now right to presume that the fact that I didn't group ,\s makes the engine ommit comma's and white space from my search. I did F around with that comma in particular, it kept showing up on the mail adresses.

Thanks in advance and greetings

2 Answers

Steven Parker
Steven Parker
210,863 Points

Your comma and space separators look good, and you didn't say what kind of problem you were having, but...

:point_right: It looks like you have mixed a group and a character class in the phone section.

I suspect the brackets and plus sign marked below don't really belong:

(?P<email>[-\w.+]+@[-\w.]+),\s(?P<phone>[\d{3}-\d{3}-\d{4}]+)
                                        ^                 ^^
Brent Liang
PLUS
Brent Liang
Courses Plus Student 2,944 Points

Hi Laurens,

Try this:

(?P<email>[-\w.+]+@[-\w.]+),\s(?P<phone>[-\d+]+)

Bad character range if you include too many in a set.

Hope it works!