Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/80.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# 只删除c上的一些html标记#_C#_Html - Fatal编程技术网

C# 只删除c上的一些html标记#

C# 只删除c上的一些html标记#,c#,html,C#,Html,我有一个字符串: string hmtl = "<DIV><B> xpto </B></DIV> string hmtl=“xpto 并且需要删除和的标记,结果是:xpto 只需和而不删除大量html标记,但保存xptohtml=Regex.Replace(html,@“”,String.Empty); 使用 如果您只是删除div标记,这将获得div标记以及它们可能具有的任何属性 var html = "<DIV>

我有一个字符串:

string hmtl = "<DIV><B> xpto </B></DIV>
string hmtl=“xpto
并且需要删除
的标记,结果是:
xpto


只需
而不删除大量html标记,但保存
xpto

html=Regex.Replace(html,@“”,String.Empty);
使用


如果您只是删除div标记,这将获得
div
标记以及它们可能具有的任何属性

var html = 
  "<DIV><B> xpto <div text='abc'/></B></DIV><b>Other text <div>test</div>" 

var pattern = "@"(\</?DIV(.*?)/?\>)"";  

// Replace any match with nothing/empty string
Regex.Replace(html, pattern, string.Empty, RegexOptions.IgnoreCase);
var html=
“xpto其他文本测试”
var pattern=“@”(\)”;
//将任何匹配项替换为nothing/空字符串
Replace(html,pattern,string.Empty,RegexOptions.IgnoreCase);
结果

<B> xpto </B><b>Other text test
xpto其他文本测试

使用
Regex

var result = Regex.Replace(html, @"</?DIV>", "");
你可以用普通的

<[(/body|html)\s]*>

在c#中:

var result=Regex.Replace(html,@“”);



您只想删除
吗?用于此类任务。@TimSchmelter您应该将其作为答案发布,这是一个示例,我不会删除很多html标记,但会保存de和。谢谢,我只接受and标记(标记为粗体)我将字符串转换为htmldocument,这是我需要使用htmldocument的库??谢谢这是一个很好的答案,但是如果我想向它传递一组标记,如
  • 等,该怎么办?假设我想创建一个函数,我可以将标记列表传递到其中(即字符串列表)?@Fernando68因为它是xpath,你可以使用多个or条件,比如
    //p |//a |//li
    @CasperLeonNielsen为什么每个人都提到关于Ooh REGEX是邪恶的老文章-不要使用post?!说真的…不是所有东西都必须通过HTML Agility Pack!那篇文章太有趣了,很痛。而且是真的。谢谢你提醒我 :)
    <B> xpto </B><b>Other text test
    
    var result = Regex.Replace(html, @"</?DIV>", "");
    
    var hmtl = "<DIV><B> xpto </B></DIV>";
    var remainTag = "B";
    var pattern = String.Format("(</?(?!{0})[^<>]*(?<!{0})>)", remainTag );
    var result =  Regex.Replace(hmtl , pattern, "");
    
    <[(/body|html)\s]*>
    
     var result = Regex.Replace(html, @"<[(/body|html)\s]*>", "");
    
    <html>
    <body>
    < / html> 
    < / body>