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

Negative Numbers Task Help

Hello,

I cant work out why my reg ex is returning numbers in pairs, and misses off the zero at the end?

Please help :(

negate.py
import re

string = '1234567890'

good_numbers = (re.findall(r"[\d][^5678]", string))

2 Answers

I was completely overthinking it, the solution was simply: good_numbers = (re.findall(r"[^567]", string))

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 67,736 Points

Glad you worked it out! Working through your original solution, the regex makes the following matches:

  • "12" since 2 is not in "5678"
  • "34" since 4 is not in "5678"
  • skips 5 since 6 is in "5678"
  • skips 6 since 7 is in "5678"
  • skips 7 since 8 is in "5678"
  • "89" since 9 is not in "5678"
  • "0" is skipped since there aren't any more characters in pattern