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 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
218,833 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))