#!/usr/bin/python
import re
str = raw_input("String containing email...\t")
match = re.search(r'[\w.-]+@[\w.-]+', str)
if match:
print match.group()
这不是最复杂的代码,如果可能的话,我正在寻找一种获取所有匹配项的方法。
请您参考如下方法:
听起来你想要re.findall() :
findall(pattern, string, flags=0)
Return a list of all non-overlapping matches in the string.
If one or more groups are present in the pattern, return a
list of groups; this will be a list of tuples if the pattern
has more than one group.
Empty matches are included in the result.
就识别电子邮件地址的实际正则表达式而言......参见this question .
另外,使用 str 作为变量名时要小心。这将隐藏 str内置。
