使用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.