只删除c#上的一些html标签
我有一个字符串:
string hmtl = " xpto
我不会采取
和
,即我的结果必须是:
xpto
谢谢
继续 –
是一个例子,我不会删除很多html标签,但保存de and xpto
。 谢谢
使用正则Regex
:
var result = Regex.Replace(html, @"?DIV>", "");
更新
正如你所提到的 ,通过这段代码,正则表达式删除了所有其他的T
var hmtl = " xpto "; var remainTag = "B"; var pattern = String.Format("(?(?!{0})[^<>]*(?)", remainTag ); var result = Regex.Replace(hmtl , pattern, "");
使用htmlagilitypack
HtmlDocument doc = new HtmlDocument(); doc.LoadHtml("yourHtml"); foreach(var item in doc.DocumentNode.SelectNodes("//div"))// "//div" is a xpath which means select div nodes that are anywhere in the html { item.InnerHtml;//your div content }
如果你只想要B标签..
foreach(var item in doc.DocumentNode.SelectNodes("//B")) { item.OuterHtml;//your B tag and its content }
如果您只是删除div标签,这将获得div标签以及它们可能具有的任何属性。
Regex.Replace(" xptoOther text test" @"(\?DIV(.*?)/?\>)", string.Empty, // Replace any match with nothing RegexOptions.IgnoreCase);
结果
xptoOther text test
你可以定期使用
<[(/body|html)\s]*>
在c#中:
var result = Regex.Replace(html, @"<[(/body|html)\s]*>", ""); < / html> < / body>
html = Regex.Replace(html,@"<*DIV>", String.Empty);