2013-07-04 36 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

相关问题