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

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 68,441 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