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

Manyung Tah
Manyung Tah
7,435 Points

regex negation help?

Why does print(re.findall(r'[\w]+[^hey]+', 'hey1234')) return 'hey1234' when I have included [^hey] to ignore the 'hey'? I thought it would return '1234'.

I understand that 'print(re.findall(r'[\w]+[^hey]+', 'hey1234'))' means match 1 or more occurrences of any number, letter, underscore that doesn't include 1 or more occurences of the letters h,e,y.

I know print(re.findall(r'[^hey]+','hey1234')) will return 1234 but why doesn't print(re.findall(r'[\w]+[^hey]+', 'hey1234'))?

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 67,989 Points

Good question! The [\w]+[^hey]+ says 1 or more alphanumeric character followed by 1 or more characters that are not “h”, “e”, or “y”.

  • The first part [\w]+ matches “hey123” as they’re all alphanumeric characters
  • The second part [^hey]+ matches “4” since it is not “h”, “e”, or “y”.

If you drop the first + to make the first part [\w], then the pattern would return “y1234” since “y” is the first alphanumeric character that isn’t followed by “h”, “e”, or “y”.

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

Manyung Tah
Manyung Tah
7,435 Points

Thank you so much for your explanation!! I understand it now :)