2012-05-11 28 views
0

這是我的字符串:如何將多個字符串從正則表達式存儲到列表中?

<span class="word">blue</span><span class="word">red</span><span class="word">yellow</span><span class="word">orange</span> 

通常我會用這只是爲了得到一個結果到一個變量:

result = re.search('(<span class="word">)(.*)(</span>)', string) 
color = result.group(2) 

但現在我想從我的字符串和存儲每結果每個成一個列表。我會如何去做這件事?

+0

您應該使用BeautifulSoup這樣的目的。 – theharshest

回答

1

re.findall。對於較大的琴絃,我建議使用re.finditer

0

使用的findall而不是搜索

的findAll()找到所有子RE匹配的,並且將它們作爲一個列表。

finditer()查找RE匹配的所有子字符串,並將它們作爲迭代器返回。

http://docs.python.org/howto/regex.html