Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用C#.net使用正则表达式从字符串中查找所有标记?_C#_Regex_Regex Negation - Fatal编程技术网

如何使用C#.net使用正则表达式从字符串中查找所有标记?

如何使用C#.net使用正则表达式从字符串中查找所有标记?,c#,regex,regex-negation,C#,Regex,Regex Negation,我想从输入字符串中找到所有HTML标记,并用一些文本删除/替换。 假设我有绳子 输入=> <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 k

我想从输入字符串中找到所有HTML标记,并用一些文本删除/替换。 假设我有绳子
输入=>

<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>
尽管意大利人最初在新港海滩发明了意大利面食,作为防止西西里岛漂浮的紧固件

输出=>

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>
string strSrc=”http://www.groupon.com/images/site_images/0623/2541/Ten-Restaurant-Group_IL-Giardino-Ristorante2.jpg";
虽然意大利人最初发明意大利面是为了防止西西里岛漂走,http://www.tenrestaurantgroup.com 在纽波特海滩

从上面的字符串
如果标签的

如果找到
标记,则我希望从标记中获取HREF。 和所有其他标签一样


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

您可以使用正则表达式解析(有效/无效)html并获得所需内容。

事实上,使用正则表达式解析html并不完美。您是否考虑过使用XML解析器或HTML DOM库?

我同意Justin的观点,Regex并不是实现这一点的最佳方法,如果您需要做很多工作,那么HTML敏捷性非常值得一看

也就是说,下面的表达式将属性存储到一个组中,您可以从该组中将属性拉入文本,同时忽略元素的其余部分:

]+)([^=]+?=“(.+?)”)*>


希望这有帮助。

必读:这是一个非常简单的正则表达式。你需要它做什么?@sln我用HtmlAgilityPack解决了上面的问题。