Reputation: 117
I'm getting None as answer for this code. When I do either email or phone alone the code works. But using both returns a none . Please help!
import re
string = 'Love, Kenneth, [email protected], 555-555-5555, @kennethlove Chalkley, Andrew, [email protected], 555-555-5556, @chalkers McFarland, Dave, [email protected], 555-555-5557, @davemcfarland Kesten, Joy, [email protected], 555-555-5558, @joykesten'
contacts = re.search(r'''
^(?P<email>[-\w\d.+]+@[-\w\d.]+) # Email
(?P<phone>\d{3}-\d{3}-\d{4})$ # Phone
''', string, re.X|re.M)
print(contacts.groupdict)
Upvotes: 0
Views: 1443
Reputation: 74655
Perhaps you want:
(?P<email>[-\w\d.+]+@[-\w\d.]+), (?P<phone>\d{3}-\d{3}-\d{4})
This matches the parts:
[email protected], 555-555-5555
[email protected], 555-555-5556
[email protected], 555-555-5557
[email protected], 555-555-5558
Upvotes: 1
Reputation: 793
You are using ^
and $
to enforce a match on the entire string. Your regexp seems designed to match only a substring.
Upvotes: 0