我正在尝试使用Python 2.7正则表达式来从我正在使用的课程中提供的示例网页中检索数据。我试图去工作的代码是:Python RE,AttributeError:'元组'对象没有属性'组'
email_patterns = ['(?P<lname>[\w+\.]*\w+ *)@(?P<domain> *\w+[\.\w+]*).(?P<tld>com)
for pattern in email_patterns:
# 'line' is a line of text in a sample web page
matches = re.findall(pattern,line)
for m in matches:
print 'matches=', m
email = '{}@{}.{}'.format(m.group('lname'), m.group('domain'),m.group('tld'))
运行此返回以下错误:
email = '{}@{}.{}'.format(m.group('lname'), m.group('domain'), m.group('tld'))
AttributeError: 'tuple' object has no attribute 'group'.
我想用命名的组,因为该组的顺序可以根据改变我正在匹配的文本。但是,它似乎不工作,因为编译器不认为'm'是一个Group对象。
这里发生了什么,以及如何通过使用命名组来正确工作?
[ “正则表达式的检索来自样品网页上的数据”(http://stackoverflow.com/questions/1732348/regex-match-open-tags- except-xhtml-self-contained-tags/1732454#1732454) – 2012-03-24 05:06:38