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 Counts

Anthony Grodowski
Anthony Grodowski
4,902 Points

\w* and \w+

I don't quite get why we didn't recieve ', Tim' by print(re.findall(r'\w+, \w+', data)), but with print(re.findall(r'\w*, \w+', data)) we did. I know what \w* searches for at least 0 Unicode characters, which comma isn't, so why it prints before Tim that comma? Shouldn't it print just Tim?

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

Good question! The pattern used must completely match any string that is returned. By using the "\w*, \w+" it says "any or no word characters, followed by a comma and space, followed by one or more word characters". So the "comma space" must be part of the match and is therefore part of the returned string.

The pattern "\w+, \w+" it says "one or more word characters, followed by a comma and space, followed by one or more word characters". So without a word character immediately preceding the "comma space", the text ", Tim" does not match the pattern.

In later videos, you will learn about ^ which anchors the pattern to the beginning of the string. This will allow catching ", Tim" but exclude matching other items within the string.

Post back if you have more questions. Good Luck!!

Anthony Grodowski
Anthony Grodowski
4,902 Points

It's clear now! Thanks! But I have a problem with ^ and $ pattern. Could you help me with that please? https://teamtreehouse.com/community/multiline-problem