使用C#定期EX pressions删除HTML标签 [英] Using C# regular expressions to remove HTML tags

查看:169
本文介绍了使用C#定期EX pressions删除HTML标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何使用C#普通EX pression替换/删除所有的HTML标记,包括尖括号?
是否有人可以帮助我的code?

How do I use C# regular expression to replace/remove all HTML tags, including the angle brackets? Can someone please help me with the code?

推荐答案

由于之前经常说,你不应该使用常规的前pressions处理XML和HTML文档。他们不使用HTML和XML文档的表现非常好,因为没有办法前preSS嵌套结构的通用方式。

As often stated before, you should not use regular expressions to process XML or HTML documents. They do not perform very well with HTML and XML documents, because there is no way to express nested structures in a general way.

您可以使用以下。

String result = Regex.Replace(htmlDocument, @"<[^>]*>", String.Empty);

这将为大多数情况下工作,但会有这样的情况(含尖括号例如CDATA),其中如预期,这将无法正常工作。

This will work for most cases, but there will be cases (for example CDATA containing angle brackets) where this will not work as expected.

这篇关于使用C#定期EX pressions删除HTML标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆