2017-01-30 63 views
0

我得到None作为此代码的答案。当我单独做电子邮件或电话时,代码就可以工作。但使用两者都返回无。请帮忙!Python REGEX搜索返回无作为答案

进口重新

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.+][email protected][-\w\d.]+) # Email 
    (?P<phone>\d{3}-\d{3}-\d{4})$ # Phone 
''', string, re.X|re.M) 

print(contacts.groupdict) 
+0

1.删除'$',''^ 2.添加',\ S * '#电子邮件前'# –

+0

感谢使用这个工作! –

回答

0

您正在使用^$执行对整个字符串匹配。你的正则表达式似乎被设计为只匹配一个子字符串。