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

David Bell
David Bell
16,981 Points

Are sets ranges inclusive for regex?

In doing the challenge for regular expressions: Negation, I'm getting a passable answer, but I don't understand how.

The attached code below, where the range is [^5-7] is failing, showing that 5 and 6 are removed, but not 7.

In the ones that have worked, I've made the negation [^5-8] or [^5-9].

So, why doesn't [^5-7] get rid of 5, 6 and 7, when [0-9] will give me all the numbers in string?

import re

string = '1234567890'

good_numbers = re.findall(r'[0-9][^5-7]', string)

1 Answer

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

I'm actually not sure on this one yet, I get the same problem. But, I think it's more what you're actually asking for (two numbers, one between 0 and 9, the other not 5, 6, or 7) that causes the weirdness. For this challenge, though, you don't need to ask for any number, just ignore certain ones...

David Bell
David Bell
16,981 Points

ooooh, right, got it. I don't need both.