Ronit Mankad
Ronit Mankad

Reputation: 117

Python REGEX search returns None as answer

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

Answers (2)

Dan D.
Dan D.

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

Debuggex Demo

Upvotes: 1

R&#233;mi Bonnet
R&#233;mi Bonnet

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

Related Questions