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 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
231,248 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!