我有這樣的文字:查找單詞下來(之間)的字符串在Python
<div style="margin-left:10px;margin-right:10px;">
<!-- start of lyrics -->
There are times when I've wondered<br />
And times when I've cried<br />
When my prayers they were answered<br />
At times when I've lied<br />
But if you asked me a question<br />
Would I tell you the truth<br />
Now there's something to bet on<br />
You've got nothing to lose<br />
<br />
When I've sat by the window<br />
And gazed at the rain<br />
With an ache in my heart<br />
But never feeling the pain<br />
And if you would tell me<br />
Just what my life means<br />
Walking a long road<br />
Never reaching the end<br />
<br />
God give me the answer to my life<br />
God give me the answer to my dreams<br />
God give me the answer to my prayers<br />
God give me the answer to my being
<!-- end of lyrics -->
</div>
我想打印首歌的歌詞,但re.findall
和re.search不要在這種情況下工作。我如何?我正在使用此代碼:
lyrics = re.findall('<div style="margin-left:10px;margin-right:10px;">(.*?)</div>', open('file.html','r').read())
for words in lyrics:
print words
[除XHTML自包含標籤的正則表達式匹配開放標籤](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) –
're.findall'和're.search'肯定仍然有效,並且會在這種情況下工作,所以你只是沒有使用正確的正則表達式。因爲你沒有發佈你要做的事情,這會讓人們很難幫助你。 – Ben