Using The RegEx Replace Method, Strip or Remove HTML Tags from Text

Using Regex,it is simple to eliminate every HTML tag by regex.Replace().

Replace-Sometimes we need to swap out a text pattern for a different text pattern. Regex. Replace is useful. We have the option of replacing patterns with a string or a MatchEvaluator-generated value.

string html = "<ol dir="rtl" style="list-style-type:lower-alpha">
    <li dir="RTL"><u>Hello World:</u></li>
</ol>";
string text = Regex.Replace(html,"").Trim();

The full example using the aforementioned code snippet is provided here-

using System.Text.RegularExpressions;
using System;

namespace ConsoleAppRemoveHtml
{
  class Program
  {
    static void Main(string[] args)
    {
      
string html = "<ol dir="rtl" style="list-style-type:lower-alpha">
    <li dir="RTL"><u>Hello World:</u></li>
</ol>";
      string text = Regex.Replace(html,"").Trim();
      Console.WriteLine(text);
    }
  }
}

Output of Program-
Hello World:

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories