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 trialBrian Anstett
5,831 Pointsre.search on multiple lines
Hello, I work on the coding challenges in a separate IDE for conviences and I'm having trouble using re.search with groups on multiple line strings. I'm getting the output that I'm expecting in terms of pulling the patterns, but only for the first line. I feel like this was mentioned in one of the videos but I can't seem to find it. Thanks for all the help!
**********************CODE**********************
string = '''Love, Kenneth, kenneth+challenge@teamtreehouse.com, 555-555-5555, @kennethlove Chalkley, Andrew, andrew@teamtreehouse.co.uk, 555-555-5556, @chalkers McFarland, Dave, dave.mcfarland@teamtreehouse.com, 555-555-5557, @davemcfarland Kesten, Joy, joy@teamtreehouse.com, 555-555-5558, @joykesten'''
line = re.search(r'(?P<email>[-\w\d+.]+@[-\w\d.]+),\s(?P<phone>\d{3}-\d{3}-\d{4})',string,re.VERBOSE|re.MULTILINE) print(line.groupdict())
Output {'email': 'kenneth+challenge@teamtreehouse.com', 'phone': '555-555-5555'}
Sabine Maennel
Treehouse Project ReviewerSabine Maennel
Treehouse Project ReviewerHello Brian,
re.search only returns the first match of you pattern. If you want to find all matches, you need re.findall():
>>> string = '''Love, Kenneth, kenneth+challenge@teamtreehouse.com, 555-555-5555, @kennethlove Chalkley, Andrew, andrew@teamtreehouse.co.uk, 555-555-5556, @chalkers McFarland, Dave, dave.mcfarland@teamtreehouse.com, 555-555-5557, @davemcfarland Kesten, Joy, joy@teamtreehouse.com, 555-555-5558, @joykesten'''
>>> re.findall(r'(?P<email>[-\w\d+.]+@[-\w\d.]+),\s(?P<phone>\d{3}-\d{3}-\d{4})',string,re.VERBOSE|re.MULTILINE)
[('kenneth+challenge@teamtreehouse.com', '555-555-5555'), ('andrew@teamtreehouse.co.uk', '555-555-5556'), ('dave.mcfarland@teamtreehouse.com', '555-555-5557'), ('joy@teamtreehouse.com', '555-555-5558')]
These are just tuples so. To get group dicts again: you can compile the pattern and then use an iterator and list comprehension, like this:
>>> regex = re.compile(r'(?P<email>[-\w\d+.]+@[-\w\d.]+),\s(?P<phone>\d{3}-\d{3}-\d{4})',re.VERBOSE|re.MULTILINE)
>>> [line.groupdict() for line in regex.finditer(string)]
[{'phone': '555-555-5555', 'email': 'kenneth+challenge@teamtreehouse.com'}, {'phone': '555-555-5556', 'email': 'andrew@teamtreehouse.co.uk'}, {'phone': '555-555-5557', 'email': 'dave.mcfarland@teamtreehouse.com'}, {'phone': '555-555-5558', 'email': 'joy@teamtreehouse.com'}]
Did that answer your question?