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 Sets

vikas pal
vikas pal
11,252 Points

explain sets in python

please explain me briefly about sets for example in the video he said that [aple] matches 'apple' .how it match apple. i tried to find apple match in a file which i created names.txt.i typed apple in last of the line but it doesn't match it .it matched the starting p and said it matches p.here is the all thing of script

import re
#print(re.findall(r'\w*, \w+',data))

and names.txt

pal, Vikas vikaspal@gmail.com (555) 555-5555 student, school, @vikas_enjoy
pal, Divya divya@gmail.com (666) 555-5554 student, college, @divya_pal
pal, Pratibha pratibha@gmail.com (444) 555-5552 student, secondary, @pratibha pal
love, kennneth kenneth@teamtreehouse.com (555) 555-5543 teacher, treehouse @kennethlove
McFarland, dave dave@teamtreehouse.com (555) 555-5553 teacher, treehouse 
Arthur, king king_artur@camelot.co.uk  king, camelot
Osterberg, sven-Erik governor@norrbotten.co.se Governor, Norrbotten @sverik
, Tim tim@killerrabbit.com Enchanter, killer Rabbit Cave
carson,Ryan ryan@teamtreehouse.com (555) 555-5534 CEO, treehouse @ryancarson
Docter, The docter+companion@tardis.co.uk   Time Lord, Gallifrey
Exampleson, Example me@example.com 555-555-5553 Example Co. @example
Obama, Barack president.44@us.gov 555 555-5551 President, United States of America @potus44
Chalkley, Andrew andrew@teamtreehouse.com (555) 555-4444 Sith Lord, Galactic Empire @darthvader

1 Answer

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

A regex set is a collection of things that can be matched. The word "apple" has 4 letters in it, "a", "p", "l", and "e". The "p" is repeated, but a repetition isn't a new letter. So the set to match the characters in the word "apple" would be [aple]. Or [pale]. Or [ealp]. The order isn't important, only the contents of the set.

Now this won't match the exact string "apple". It's just matching whether or not the characters appear. To match exactly "apple", you should search for exactly "apple". re.search(r"apple") instead of re.search(r"[aple]+").

Also, your search in your example isn't using the +. I don't remember if that's been covered in the course at this point or not, but that will let you match 1 or more characters in a row, not just a single character.

vikas pal
vikas pal
11,252 Points

thank you so much .You cleared my all doubt about set.very helpful to me.again thanks