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

Mar Bocatcat
Mar Bocatcat
7,405 Points

Re.Expressions

What am i missing here? Any advice?

negate.py
import re

string = '1234567890'

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

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 67,736 Points

Hi Mar,

There are two issues with your solution. First, the task asks only to create a variable named good_numbers, not a function.

Second, your regex is overly complicated for the task. By combining two character sets your regex r'[\w\d][^567]' says:

`[any word or decimal character][not followed by 5 6 or 7]' this means the "7" will still be found "good" because it is follow by "8".

Your regex need only say `[not a 5, 6 or 7]'. This can be accomplished using:

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

The re.I is not needed since there is no character case to ignore.

Best of luck. Keep Coding!

Mar Bocatcat
Mar Bocatcat
7,405 Points

Thank you ! I need to read the details more!