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

Why does this exclude not work? It keeps returning 7 with it.

It keeps returning 7 when I have expressed to leave it out.

negate.py
import re

string = '1234567890'

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

1 Answer

Dan Johnson
Dan Johnson
40,531 Points

Since you have a \d in your regular expression along with the character set [^567] you're saying, "capture any one digit, and then any character that is not 5, 6, or 7." This is why you can end up with one number that falls in the excluded character set.

So all you need to do is remove the \d and you should capture the correct groups.

Chase Swanson
Chase Swanson
Courses Plus Student 8,886 Points

Dan, you are correct, but can you explain more the theory of why the original code up above is pulling pairs instead of each digit by itself?

The above code produces ['12', '34', '78', '90']

I am not exactly following the logic. Everything else in the section has made sense, but I must be missing something.

Dan Johnson
Dan Johnson
40,531 Points

Your get pairs since \d is looking to match one digit, and [^567] is looking to match a separate character that is not excluded. The character set is not a modifier for \d, it's for the character after a digit that has already been found.

We can walk through the string to see how it matches:

  • '1' matches as it is a digit. It is followed by '2' which is not excluded by the set [^567] so it matches, making our first complete match "12".
  • '3' is a digit, and '4' is not excluded. Our next match is "34".
  • '5' is a digit, but now '6' is next and this does fall in the excluded character set, so this can't match.
  • '6' is a digit, but we have the same issue as the last pair with '7' being excluded, so no match.
  • '7' is a digit, and '8' lies outside of the excluded range giving us our next match, "78".
  • '9' is a digit, and '0' isn't excluded. This gives us our final match of "90".