Introduction

Dealing with HTML content often requires extracting plain text for processing, analysis, or display purposes without the clutter of HTML tags. In this blog, we'll explore a simple yet effective method using regular expressions (Regex) in C# to strip HTML tags and decode HTML entities to plain text. This technique is particularly useful in scenarios like reading content from web scraping, cleaning up email formats, or preparing textual data for machine learning preprocessing.

Problem Statement

HTML content is designed for web browsers, not for straightforward text processing. Extracting just the textual part can be tricky due to the nested and intricate nature of HTML tags. Developers need a reliable method to convert HTML to plain text efficiently.

Solution Overview

We will use the C# Regex.Replace the method to remove HTML tags and System.Net.WebUtility.HtmlDecode to decode HTML-encoded entities to their text equivalents. This approach provides a quick and accurate way to extract clean text from HTML.

Define the Text Extraction Method

Code Walkthrough

string htmlContent = "<p>Hello <b>World!</b></p>";
string plainText = ExtractTextFromHtml(htmlContent);
Console.WriteLine(plainText); // Outputs: Hello World!

public string ExtractTextFromHtml(string html)
{
    if (html == null)
    {
        return "";
    }

    string plainText = Regex.Replace(html, "<[^>]+?>", " ");
    plainText = System.Net.WebUtility.HtmlDecode(plainText).Trim();

    return plainText;
}

Explanation

Conclusion

By following the above steps, developers can effectively extract text from HTML content using a straightforward Regex-based method in C#. This functionality is essential for applications that need to process or display text extracted from HTML sources, ensuring clarity and usability of the data.

This guide provides a practical solution to a common problem faced in text processing, making it a valuable addition to your development toolkit. Whether you are working on web scraping, data cleaning, or content management systems, knowing how to convert HTML to plain text efficiently is a crucial skill.