2013-07-04 37 views
4

我想匹配一個html代碼,直到下一次出現...或結束。匹配所有內容,直到下一個匹配

目前,我有以下的正則表達式:

(<font color=\"#777777\">\.\.\. .+?<\/font>) 

將匹配這一點:

1. <font color="#777777">... </font><font color="#000000">lives up to the customer's expectations. The subscriber is </font> 
2. <font color="#777777">... You may not want them to be </font> 
3. <font color="#777777">... </font><font color="#000000">the web link, and </font> 

但我想:

1. <font color="#777777">... </font><font color="#000000">lives up to the customer's expectations. The subscriber is </font><font color="#777777">obviously thinking about your merchandise </font><font color="#000000">in case they have clicked about the link in your email.</font> 
2. <font color="#777777">... You may not want them to be </font><font color="#000000">disappointed by simply clicking </font> 
3. <font color="#777777">... </font><font color="#000000">the web link, and </font><font color="#777777">finding </font><font color="#000000">the page to </font><font color="#777777">get other than </font><font color="#000000">what they thought it </font><font color="#777777">will be.. If America makes</font> 

這裏是我想要的HTML解析:

<font color="#777777">... </font><font color="#000000">lives up to the customer's expectations. The subscriber is </font><font color="#777777">obviously thinking about your merchandise </font><font color="#000000">in case they have clicked about the link in your email.</font><font color="#777777">... You may not want them to be </font><font color="#000000">disappointed by simply clicking </font><font color="#777777">... </font><font color="#000000">the web link, and </font><font color="#777777">finding </font><font color="#000000">the page to </font><font color="#777777">get other than </font><font color="#000000">what they thought it </font><font color="#777777">will be.. If America makes</font> 

與示範: http://rubular.com/r/mmQ4TBZb96

如何匹配所有文字開始......得到高於期望的比賽嗎?

感謝您的幫助!

+1

問題描述得很差。當你說所有的字符串時,你的意思是__font__標籤內的字符串嗎?你期望什麼樣的輸出 – tr33hous

+1

你的預期匹配輸出是什麼? – anubhava

+0

你想加入短語嗎? – mzmm56

回答

2

即使您的問題似乎不一致的(我不明白你爲什麼會獲得最終所需的匹配),我認爲這是你追求的:

((<font color=\"#777777\">\.{3}) .+?(<\/font>(?=\s*\2)|$)) 

它採用了先行以使比賽結束是下一個「...」序列(或輸入剛剛結束

this on rubular

+0

嘿,這是我正在尋找的,但它不會找到最後一場比賽(其中只有兩個) – Aljaz

+0

這看起來不錯,雖然Rubular沒有把它做對(它只顯示第一場比賽),在來自Angga的鏈接上方是正確的。 – Armali

+0

當我用ruby運行它時,我得到的輸出與rubular相同 - 沒有最後一場比賽。你有什麼想法可能會造成這種情況? – Aljaz

0

的問題是關於正則表達式,但你也可以做到這一點在以下方式(Perl系統ntax,但我相信這種功能也存在於其他語言中):

split(/(?=<font color=\"#777777\">\.\.\.)/, $your_text) 
+0

這隻會匹配字體標籤中的內容,而不是標籤後的內容 – Aljaz

+0

對不起,我不明白這個問題。 @Bohemian的答案看起來很棒(我無法做得更好),但由於我已經發布了一個答案,我需要提供替代方法。 – Vasiliy

相關問題