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

negate.py help

I'm not sure I understand what order to put things in. I tried a bunch of combinations using the plus sign and a negation set, but I keep getting the original string or an empty string. Or, sometimes I get numbers broken between commas, and for some reason the number 7 is always still there. Any help is appreciated.

negate.py
import re

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

2 Answers

Jeffrey James
Jeffrey James
2,636 Points

You're pretty close - there's no need for the \d, which recognizes a digit generally https://stackoverflow.com/a/6479605/3182843

The [^567] reads NOT match a 5 or 6 or 7

>>> import re
>>> string = '1234567890'
>>> # Create a variable named good_numbers that is an re.findall() where the pattern matches anything in string except the numbers 5, 6, and 7.
... 
>>> re.findall(r'[^567]', string)
['1', '2', '3', '4', '8', '9', '0']

had you something like this and wanted to only return digits

>>> data = "jeffrey34"
>>> re.findall(r'\d+', data)
['34']
>>> 

Thanks for the reply. My next question is why does something like r'[^567]+, string

return ['1234', '890]

I though it would make it into a continuous string, like ['1234890']

Jeffrey James
Jeffrey James
2,636 Points

The + operator is a repetition character. So if you're saying to match all blocks of non 5s, 6s or 7s, it finds 2 of them in your original string. To be honest, that's fairly obscure. Usually, at least in my experience, when you write regexp, you inevitably have to fool around to get it correct, especially on non trivial problems. You also may not fully understand what happened, but you can verify that it worked :)

>>> re.findall( r'[^567]', string)
['1', '2', '3', '4', '8', '9', '0']
>>> re.findall( r'[^567]+', string)
['1234', '890']