SoFunction
Updated on 2025-03-01

C# method to filter HTML tags regularly and retain specified tags

This article describes how C# regularly filters HTML tags and retains the specified tags. Share it for your reference, as follows:

Here we mainly see a filtering function:

public static string FilterHtmlTag(string s)
{
 //<...> Mark regular expression return (s, @"&lt;[^&gt;]*&gt;", delegate(Match match)
 {
  string v = ();
  //Picture,<p>,<br>regular expression  Regex rx = new Regex(@"^&lt;(p|br|img.*)&gt;$",
    | ); //
  if ((v))
  {
  return v; //Reserve the picture,<p>,<br>  }
  else
  {
  return ""; //Filter out  }
 });
}

All of my filters are filtered here, so I use the regular directly and no longer keep p and br for anonymous delegation

content = (content, @"/\<span(\sclass\=\S*)*\>\S*\<\/span\>/g", "", );
content = (content, @"<[^>]*>", "", );
content = content + "。。。";

PS: Here are two very convenient regular expression tools for your reference:

JavaScript regular expression online testing tool:
http://tools./regex/javascript

Regular expression online generation tool:
http://tools./regex/create_reg

For more information about C#, please visit the special topic of this site:Summary of the usage of C# regular expressions》、《Summary of C# coding operation skills》、《Tutorial on the usage of common C# controls》、《Summary of WinForm control usage》、《C# data structure and algorithm tutorial》、《Introduction to C# object-oriented programming tutorial"and"Summary of thread usage techniques for C# programming

I hope this article will be helpful to everyone's C# programming.