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.
Andy McDonaldPython Development Techdegree Student 8,745 Points
Trouble negating numbers from a string
string = '1234567890'
def good_numbers(stri): return re.findall(r'''[\d][^5678]''', stri, re.X|re.I)
I need a function that returns all numbers that arent 5, 6, and 7. I got to this function from realizing I'm utterly out of ideas on what to do. I noticed that the last number in the negated set still shows up in the string... So my question is:
- Why is the last number in the set not negated?
- When doing a findall on \d alone. It returns each individual number. But as soon as a add the negated set. It returns them in double digits.... ie 12, 34,ect instead of 1, 2, 3, 4. I'm really curious as to why that is happening.
- And finally, what it is that I'm doing wrong??? What it is it about this function that isn't right? Why cant I get 1, 2, 3, 4, 8, 9, 0?
import re string = '1234567890' def good_numbers(stri): return re.findall(r'''[\d][^5678]''', stri, re.X|re.I)
Chris FreemanTreehouse Moderator 68,063 Points
Hey Andy McDonald, you’re on the right path but the structure is not correct.
- the challenge asks to define a variable, not define a function. Use the form
good_numbers = re.findall(...
r‘[\d][^5678]', you are matching two characters at a time. This regex says “match any digit that isn’t followed by a 5, 6, 7, or 8”.
By looking at pairs of numbers, the pattern “consumes” both numbers, so only five matching attempts are done in a string of ten characters:
- “12”, passes since 2 is not in “5678”
- “34”, passes since 4 is not in “5678”
- “56”, fails since 6 is in “5678”
- “78”, fails since 8 is in “5678”
- “90”, passes since 0 is not in “5678”
What you want instead is “any single character that is not 5, 6, 7”, that is,
- you can use ’ instead of ’’’, since you don’t a multiple line pattern
- neither of the re options are required since you are not using verbose mode and do not need to ignore case
Post back if you need more help. Good luck!!!