2017-01-30 61 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

您正在使用^$執行對整個字符串匹配。你的正則表達式似乎被設計爲只匹配一個子字符串。