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

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 68,457 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!!!

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