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 Negated Numbers

dylan hamm
dylan hamm
3,667 Points

I don't really know how to do this one

I have attempted to find all numbers and ignore only 5, 6, and 7 however the code finds all numbers and includes 5,6, and 7

negate.py
import re

string = '1234567890'

good_numbers = re.findall(r'\d*[^567]', string)

2 Answers

Stuart Wright
Stuart Wright
41,101 Points

Your regular expression reads:

"Match any number of digits, followed by one character that is not 5, 6 or 7."

This should do what the challenge is asking, which is match any number of characters except 5, 6 or 7 :

import re

string = '1234567890'

good_numbers = re.findall(r'[^567]*', string)
dylan hamm
dylan hamm
3,667 Points

I figured it out shortly after posting the question but I appreciate the help

Tri Pham
Tri Pham
18,671 Points

I didn't like the way the question was phrased. I thought they should have added the expected result. The expected result is actually ['1', '2', '3', '4', '8', '9', '0']. Yours got ['1234567890']. Can you solve it now?

Tri Pham
Tri Pham
18,671 Points

actually now I go no clue what they wanted :(. Stuart's answer works too and produces ['1234', '', '', '', '890', ''].... my solution was good_numbers = re.findall(r'[^567]', string) and produced ['1', '2', '3', '4', '8', '9', '0'].

Stuart Wright
Stuart Wright
41,101 Points

Yes good point, it's not very clear what they want. Reading the question again I think yours is the solution they're after, but it's odd that mine gets accepted too.