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

angel moreta
angel moreta
2,912 Points

i need a hint =D

why is not excluding 567 ?

output:['1234567890'] i know im pretty close so guys please a good explanation would be great help , something else: since in my string var are only numbers i didnt bother using \w and i used \d instead so i dont know if that might cause not removing 567 :/

negate.py
import re

string = '1234567890'

def good_numbers(string):
    return re.findall(r'[\d]+[^567]',string)

1 Answer

Brandon Spangler
Brandon Spangler
8,756 Points

Hint one: The challenge says to find a variable, it doesn't say to write a function!

Hint two: So, what it looks like findall() does is search through the string from left to right searching for the pattern specified. You have asked it to find a pattern that is possibly endless set of numbers with one number on the end of the pattern that is not 5,6, or 7. So it's returning ['1234567890'] which is the pattern you have specified. The [\d]+ says to python "look for a set of one or more numbers that ends with one number that is not 5,6,or 7"; so 1,2,3,4,5,6,7,8, and 9 are being "eaten" by [\d]+ and the [^567] returns the 0 at the end. If the string ended in, say, 5, this new string would not have met your specifications. This might sound confusing and I don't know how to say it exactly, but if you tell python to look for the pattern "one number that is not 5,6, or 7", findall() will look through the string and return each number (in a string) that is not 5,6, or 7. So, if you define your pattern correctly, python will look at each number individually, decide if it is 5,6,or 7 and it will add the number, as a string, to a list.

I hope this sort of helps lol. I'm sure I made no sense though. I used https://www.debuggex.com/cheatsheet/regex/python and https://docs.python.org/3/howto/regex.html to help me solve this particular challenge. Good luck!

angel moreta
angel moreta
2,912 Points

thank you man i just solved it, thanks for your time!

Brandon Spangler
Brandon Spangler
8,756 Points

My pleasure! I was stuck on this one too lol. Hopefully the hints helped haha