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
William Sternlicht
Courses Plus Student 11,197 PointsUsing Regex to exclude certain patterns?
Hi! I've been trying for more time than I'd like to admit, to figure out a regex expression to get all words from a string excluding certain words:
I've been using this awesome site to check my work: http://pythex.org
I.e. if i have the string "good great bad gOod gooooood bad goood bad good awesome bad bad gooD bad"
and i want a list of all the elements that are not "bad"
how do i do that?
right now i'm stuck pretty close at using [A-Za-z'][^bad][A-Za-z']*
which returns: good, great, d gOod, gooooood, d goood, d good, awesome, d bad, gooD"
It leaves in the bad's d though, as well as any repetitions of "bad"
Thanks for taking the time to help!
1 Answer
man odell
1,905 Pointsyour_string= "good great bad gOod gooooood bad goood bad good awesome bad bad gooD bad" def string_factory(any_string): s=any_string.lower() s1=s.split() new_string=[] for i in range(0,len(s1)): if not s1[i]=='bad': new_string.append(s1[i]) return new_string