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

Dot "." appearance in the result from regular expression

I was playing with this part of code and didn't get how the last line affects appearance of a . in the result.

print(re.findall(r'''
    \b[-\w]+,  
    \s 
    [-\w ]+ 
    [^\t\n]  
''', data, re.X)) 

This dot appears in this string ,right after Co - Example, Example Co., if last part [^\t\n] is commented . does not appear in result, but when added, dot is there. Could someone please explain why?

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,426 Points

Hey Vladimir Datsenko, good question. It shows small details can have a big impact.

The key is correctly interpreting the last character set [^\t\n]. The caret ^ is misplaced. When used as the first character in a character set, it negates the rest of the set. This means it will match any character except tab and newline. So the period gets matched.

Removing the caret will likely give you the behavior you’re expecting.

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