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.
Matching ', Tim'
It took me a while and a lot of playing with lookbehind assertions before I figured out that if you want to also match
', Tim' in the address book, you just need to remove the first
\b (word boundary).
print(re.findall(r''' [-\w]*, # Find a word boundary, 1+ hyphens or characters, and a comma \s # Find 1 whitespace [-\w ]+ # 1+ hyphens and characters and explicit spaces [^\t\n] # Ignore tabs and newlines ''', data, re.X))
And now that I've progressed through the Groups step and learnt about the multiline flag (
re.M), I've realised that the following will return only the names, not the jobs/companies:
print(re.findall(r''' ^[-\w]*, # Find 1+ hyphens or characters, and a comma \s # Find 1 whitespace [-\w ]+ # 1+ hyphens and characters and explicit spaces [^\t\n] # Ignore tabs and newlines ''', data, re.X|re.M))
Note the caret (
^) at the start of the expression to only match from the start of each line.