从string中移除HTML标记,包括C#中的 

如何在C#中使用正则expression式来移除所有的HTML标签。 我的string看起来像

"<div>hello</div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div>" 

如果您不能使用面向HTMLparsing器的解决scheme来过滤掉标签,那么下面是一个简单的正则expression式。

 string noHTML = Regex.Replace(inputHTML, @"<[^>]+>|&nbsp;", "").Trim(); 

理想情况下,你应该再次通过一个正则expression式filter来处理多个空格

 string noHTMLNormalised = Regex.Replace(noHTML, @"\s{2,}", " "); 

我拿@Ravi Thapliyal的代码做了一个方法:它很简单,可能不会清理所有东西,但到目前为止,它正在做我所需要的。

 public static string ScrubHtml(string value) { var step1 = Regex.Replace(value, @"<[^>]+>|&nbsp;", "").Trim(); var step2 = Regex.Replace(step1, @"\s{2,}", " "); return step2; } 

我一直在使用这个函数。 删除几乎任何杂乱的HTML,你可以扔在它,并保持文本完好无损。

  private static readonly Regex _tags_ = new Regex(@"<[^>]+?>", RegexOptions.Multiline | RegexOptions.Compiled); //add characters that are should not be removed to this regex private static readonly Regex _notOkCharacter_ = new Regex(@"[^\w;&#@.:/\\?=|%!() -]", RegexOptions.Compiled); public static String UnHtml(String html) { html = HttpUtility.UrlDecode(html); html = HttpUtility.HtmlDecode(html); html = RemoveTag(html, "<!--", "-->"); html = RemoveTag(html, "<script", "</script>"); html = RemoveTag(html, "<style", "</style>"); //replace matches of these regexes with space html = _tags_.Replace(html, " "); html = _notOkCharacter_.Replace(html, " "); html = SingleSpacedTrim(html); return html; } private static String RemoveTag(String html, String startTag, String endTag) { Boolean bAgain; do { bAgain = false; Int32 startTagPos = html.IndexOf(startTag, 0, StringComparison.CurrentCultureIgnoreCase); if (startTagPos < 0) continue; Int32 endTagPos = html.IndexOf(endTag, startTagPos + 1, StringComparison.CurrentCultureIgnoreCase); if (endTagPos <= startTagPos) continue; html = html.Remove(startTagPos, endTagPos - startTagPos + endTag.Length); bAgain = true; } while (bAgain); return html; } private static String SingleSpacedTrim(String inString) { StringBuilder sb = new StringBuilder(); Boolean inBlanks = false; foreach (Char c in inString) { switch (c) { case '\r': case '\n': case '\t': case ' ': if (!inBlanks) { inBlanks = true; sb.Append(' '); } continue; default: inBlanks = false; sb.Append(c); break; } } return sb.ToString().Trim(); } 
 var noHtml = Regex.Replace(inputHTML, @"<[^>]*(>|$)|&nbsp;|&zwnj;|&raquo;|&laquo;", string.Empty).Trim(); 

这个:

 (<.+?> | &nbsp;) 

会匹配任何标签或&nbsp;

 string regex = @"(<.+?>|&nbsp;)"; var x = Regex.Replace(originalString, regex, "").Trim(); 

那么x = hello

清理Html文档涉及很多棘手的事情。 这个包可能有帮助: https : //github.com/mganss/HtmlSanitizer

 (<([^>]+)>|&nbsp;) 

你可以在这里testing它: https : //regex101.com/r/kB0rQ4/1