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 trialFerenc Vámos
5,047 PointsFew questions
line = (re.search(r'''
^(?P<name>[-\w ]*,\s[-\w ]+)\t # Last and first names
(?P<email>[-\w\d.+]+@[-\w\d.]+)\t # Email
(?P<phone>\(?\d{3}\)?-?\s?\d{3}-\d{4})?\t # Phone number
(?P<job>[\w\s]+,\s[\w\s.]+)\t? # Job and company
(?P<twitter>@[\w\d]+)?$ # Twitter
''', data, re.X|re.MULTILINE))
How come that if I make one typo in the re.findall()/re.search it returns an empty list? Why doesn't it just skip over the part with the error, for example the e-mails?
The created tuples have one from each of my groups in case of using re.findall() instead of re.search(), specified between parentheses, am I correct?
How come .gropudict() works, but this code doesn't? Doesn't re.search() return a dictionary? What exactly is the difference between .groupdict() and my (wrong) solution?
for key, value in line.items():
print(f"{key}: {value}")
1 Answer
AJ Tran
Treehouse TeacherTo answer your first question
- How come that if I make one typo in the re.findall()/re.search it returns an empty list? Why doesn't it just skip over the part with the error, for example the e-mails?
When the computer reads your code, it will give you exactly what you ask of it. If your code is syntactically correct, the computer will just run the whole thing, even if there is a "logical" error like a small "variation" in a RegEx. So you get an empty list because there is nothing found with that pattern you specified.
This is one of the "features" of programming -- the computer just does what you tell it to! So to answer your second question, it will not skip the "variation" because you did not give it instructions to skip it.
I am calling it a "variation" instead of an error because there is a branch of programming you can learn called "exception handling" which deals exactly with catching errors and running "a backup plan" for when there is an error. But in this case, an "error" will be something that will otherwise cause your program to crash. :)
Ferenc Vámos
5,047 PointsFerenc Vámos
5,047 PointsFound this resource which mostly clears up my third question. I'll leave the URL here if anyone has the same issue link