为什么这个Python正则表达式代码只找到空结果?

时间:2012-11-24 00:41:43

标签: python html regex parsing css

我的代码是:

    matches = re.search('(<meta.*?>)', contents, re.DOTALL)
    if matches:
        for group in matches.groups():
            metas.append(group)
    title = re.search('(<title>.*?</title>)', contents, re.DOTALL)
    if title.groups():
        found_title = title.group(1) + '\n'
    else:
        found_title = ''

它正在使用包含元标题和标题标签的HTML页面(小写),因此我希望元标记和非空标题有多个匹配。在正则表达式周围添加或删除括号似乎没有什么区别。

1 个答案:

答案 0 :(得分:2)

re.search搜索第一场比赛。您需要使用re.findallre.finditer