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

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,118 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)

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,118 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.