2011-04-23 98 views
-1

嘿大家, 我在另一个编码冒险。我今天早些时候开始自学了一些基本的RegEx,并且创建了一个C#应用程序,它输入一个HTML文件和一个RegExes列表框,然后使用这些RegEx来替换或删除HTML标签。 我设法使一些正常工作的RegExes清理并移除乱抛垃圾表的标签,但我还需要删除硬编码css样式的混乱,并将其替换为对外部参考的引用。 经过大量的试验和错误,我终于想出了一些从<style type="text/css"></style>中选择的东西,但由于某种原因,它完全跳过了单独的样式标签块。尽管如此,它在最后一个结束时停止。 这是一个比需要的信息更好奇,这应该现在工作正常,因为我可以将与单一<link>相匹配的内容替换为外部CSS。 截至目前,我正则表达式是这样的:RegEx匹配的HTML风格标签打开,内容和关闭

<style((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)>(.*?\r\n)*(</style>) 

上半年从here拍摄,中间位是我奋斗最有,因为我已经忘记了\ r \ n和当然的了关闭标签是逐字的。

就像我说的,这工作得很好,我唯一的疑虑是,这种代码:

<style type="text/css"> 
<!-- 
#wrapper #content #main2col .modbox tr td { 
    color: #3366cc; 
    border-top-style: solid; 
    border-right-style: solid; 
    border-bottom-style: solid; 
    border-left-style: solid; 
} 
#wrapper #content #main2col .modbox tr td p em { 
    color: #0a304e; 
} 
#wrapper #content #main2col .modbox tr td em br { 
    color: #0a304e; 
} 
#wrapper #content #main2col .modbox tr td em strong { 
    color: #0a304e; 
} 
#wrapper #content #main2col p strong { 
    color: #0a304e; 
} 
#wrapper #content #main2col table tr td strong { 
    color: #0a304e; 
} 
--> 
</style> 
<style type="text/css"> 
<!-- 
table.modbox { 
    font-size:9pt; 
    font-HCMmily:"Calibri", "sans-serif"; 
    border-top-style: solid; 
    border-right-style: solid; 
} 
p.modbox { 
    margin-top:0in; 
    margin-right:0in; 
    margin-bottom:10.0pt; 
    margin-left:0in; 
    line-height:normal; 
    font-size:11.0pt; 
    font-HCMmily:"Calibri", "sans-serif"; 
} 
#wrapper #content #main2col .modbox tr .modbox { 
    color: #09C; 
    font-style: normal; 
} 
#wrapper #content #main2col .modbox { 
    color: #3366cc; 
} 
#wrapper #content #main2col .modbox { 
    color: #3a5774; 
} 
#wrapper #content #main2col .modbox tr .modbox .MsoNormal .modbox { 
    color: #3a5774; 
} 
#wrapper #content #main2col .modbox { 
    color: #3a5774; 
} 
--> 
</style> 
<style type="text/css"> 
<!-- 
table.MsoTableGrid { 
    border:solid; 
    font-size:11.0pt; 
    font-HCMmily:"Calibri", "sans-serif"; 
} 
p.MsoNormal { 
    margin-top:0in; 
    margin-right:0in; 
    margin-bottom:5pt; 
    margin-left:0in; 
    line-height:normal; 
    font-size:10pt; 
    font-HCMmily:"Calibri", "sans-serif"; 
} 
--> 
</style> 
<style type="text/css"> 
<!-- 
table.modbox { 
font-size:10.0pt; 
font-family:"Times New Roman","serif"; 
} 
--> 
</style> 

只有一个匹配,返回。我想弄清楚为什么它没有抓住</style>的拳头关闭标签。为了记录,我尝试添加(\ r \ n)?关闭标签位后,但没有任何区别。

今天是我第一次与RegEx合作,所以我真的很陌生,我可能会犯一个非常简单的错误。

任何人都可以解释我做错了什么吗?任何援助非常感谢!

+0

HTML与正则表达式解析通常是不好的主意:http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – Alex 2011-04-23 08:03:30

+0

有上面的正则表达式的第二个问题:闭合样式标签永远不会匹配。它必须是(<[/]style>)匹配反斜杠! – sebilasse 2017-04-07 09:54:52

+0

不要对HTML标签使用正则表达式!使用解析器代替... – c24b 2017-10-30 15:53:45

回答

3

我会说你有你的正则表达式的贪婪问题。最有可能的是,你应该检查所有的星星(*)和加号(+),在他们后面添加一个询问标记(?)。像

(.*?\r\n)* => (.*?\r\n)*? 

在一个侧面说明,想要解析HTML/XML与正则表达式是一个坏主意,为什么不使用一个简单的HTML解析器和检索标签的内容?

+0

贪婪你说?我曾看过那篇文章出现在我阅读的几篇文章中,但从未完全理解,我会做一些进一步的研究并尝试。 RegEx是首先想到的,再加上我一直想学习它的一些基本知识。我曾在这里看到过其他一些问题,提到用HTML做不好的选择,因为它不是普通的语言,解析器更好。虽然,我不知道如何使用解析器,所以我也会研究它。 如果您的建议有效,我会接受您的答案。感谢您的快速和有益的回应! – Omega192 2011-04-23 09:07:27

+0

果然,这一个人物变化使它正常工作。我有一种感觉,它会是一件非常简单的事情,哈哈。 非常感谢!接受的答案:] – Omega192 2011-04-23 09:43:24

+1

正则表达式的默认行为如果贪婪:为每个组匹配尽可能多的mutch,为了改变它,你添加一个?在乘号(*或+)后,它表示匹配,但保持组尽可能小以允许进一步匹配。 – Bruce 2011-04-23 10:03:41