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 trialDevin Hight
3,141 PointsWhy is it that the one space after the [\w ] makes all the difference?
What difference did the space on the first name expression make? Why did it work with the space, but not without one?
import re
string = 'Perotto, Pier Giorgio'
names = re.match(r'([\w]*),\s([\w ]*)',string, re.I)
#names = re.match(r'([\w]*),\s([\w]*)',string, re.I) the code I had before I figured out the space
2 Answers
Chris Freeman
Treehouse Moderator 68,457 PointsThe space after the second \w
is needed to match with the space in between names "Pier Giorgio
".
Post back if you have more questions. Good luck!!!
Peter Vann
36,427 PointsHi Devin!
You need the space you discovered because the first name 'pier' is the first name and the only word in the string with an immediate trailing space.
This also passed for me:
import re
string = 'Perotto, Pier Giorgio'
names = re.match(r'([\w, ]*),\s([\w\s]*)',string, re.I)
So did this:
import re
string = 'Perotto, Pier Giorgio'
names = re.match(r'([\w,]*),\s([\w\s]*)',string, re.I)
More info:
https://www.w3schools.com/python/python_regex.asp
https://www.youtube.com/watch?v=R1PcJfzsUU0
I hope that helps.
Stay safe and happy coding!