2012-11-24 78 views
0

我的代碼是:爲什麼這個Python正則表達式代碼只找到空結果?

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頁面,所以我期望的meta標籤和非空標題多個匹配。在正則表達式周圍添加或刪除括號似乎沒有什麼區別。

回答

2

re.search搜索第一個匹配項。您需要使用re.findallre.finditer

相關問題