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

David Bell
David Bell
16,997 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?

negate.py
import re

string = '1234567890'

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

1 Answer

Kenneth Love
STAFF
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,997 Points

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

Thanks!