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
Manyung Tah
7,445 Pointsregex 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
Treehouse Moderator 68,468 PointsGood 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
7,445 PointsManyung Tah
7,445 PointsThank you so much for your explanation!! I understand it now :)