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 trialvikas pal
11,563 Pointsexplain 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
name=open('names.txt')
data=name.read()
name.close()
first_name=r'Vikas'
last_name=r'pal'
#print(re.match(first_name,data))
#print(re.search(last_name,data))
#print(re.findall(r'\(?\d{3}\)?-?\s?\d{3}-\d{4}',data))
#print(re.findall(r'\w*, \w+',data))
#print(re.findall(r'[\w\d+.]+@[-\w\d+.]+',data))
print(re.search(r'[aple]',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
apple
vikas pal
11,563 Points1 Answer
Kenneth Love
Treehouse Guest TeacherA 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
11,563 Pointsthank you so much .You cleared my all doubt about set.very helpful to me.again thanks
vikas pal
11,563 Pointsvikas pal
11,563 PointsKenneth Love