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 Negation

Daniel Petrov
Daniel Petrov
3,495 Points

Same results

Skipping the second and fourth lines brings the same results. Why would we use them? @Keneth Love

Steven Parker
Steven Parker
231,269 Points

It doesn't look like your tag for Kenneth Love was correctly converted into a link. This one should work.

And I don't know which "lines" you are referring to, but perhaps he will.

3 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Hmm, OK. That's totally possible! But why have those lines? Well, partly for explicitness (especially line 3) and partly for clean data (line 5).

If, somehow, I had a string like hello-there,what-is-happening, I wouldn't necessarily want it to match. The regex would prevent that because there's no whitespace after the comma. Also, if a line does end with a tab or newline, I don't want that tab or newline to be included in my results because they're not useful characters.

Daniel Petrov
Daniel Petrov
3,495 Points

Yes, that makes sense! Thanks!

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

I'm not sure exactly what you mean by "second and fourth lines". Can you show me an example?

Daniel Petrov
Daniel Petrov
3,495 Points

Sorry my bad, it is actually 3 and 5. I am getting the same result with and without these two lines.

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