2011-03-02 80 views
0

我想从输入字符串中找到所有的HTML标签,并用一些文本删除/替换。 假设我有串
INPUT =>如何使用C#.net使用RegEx查找字符串中的所有标记?

<img align="right" src="http://www.groupon.com/images/site_images/0623/2541/Ten-Restaurant-Group_IL-Giardino-Ristorante2.jpg" /><p>Although Italians originally invented pasta as a fastener to keep Sicily from floating away, <a href="http://www.tenrestaurantgroup.com/">Il Giardino Ristorante</a> in Newport Beach.</p> 

OUTPUT =>

string strSrc="http://www.groupon.com/images/site_images/0623/2541/Ten-Restaurant-Group_IL-Giardino-Ristorante2.jpg"; 

<p>Although Italians originally invented pasta as a fastener to keep Sicily from floating away, http://www.tenrestaurantgroup.com in Newport Beach.</p> 

从上面的字符串
如果<IMG>标签,然后找到我想要得到SRC的标签,
如果发现<A>标记,那么我想从标记中获得HREF。 和所有其他标记一样,它是..

我该如何在C#.net中使用正则表达式?

+1

强制阅读:[RegEx匹配除XHTML自包含标记以外的开放标记](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) – 2011-03-02 08:54:48

+0

这是一个非常简单的正则表达式。你需要什么? – sln 2011-03-09 02:18:21

+0

@sln我使用HtmlAgilityPack来解决上述问题。 – 2011-03-09 05:50:33

回答

0

您可以使用HtmlAgilityPack解析(有效/无效)的html,并得到你想要的。

0

我同意贾斯汀,正则表达式真的不是最好的方式来做到这一点,如果这是你需要做的很多东西,HTML敏捷是值得一看的。

就是这样,下面的表达式将把属性存储到一个组中,从那里你应该能够把它们拉到你的文本中,而忽略元素的其余部分。 :

< /([^>] +)([^ =] + = “(+)。?”?)* >

希望这有助于。

相关问题