Malik.Akhtar
Malik.Akhtar

Reputation: 29

How can i remove HTML Tags from String by REGEX?

I am fetching data from Mysql but the issue is "HTML tags i.e.

&lt;p&gt;LARGE&lt;/p&gt;&lt;p&gt;Lamb;<br>;li;ul;&nbsp;

also being fetched with my data i just need "LARGE" and "Lamb" from above line. How can I separate/remove HTML tags from String?

Upvotes: 1

Views: 3074

Answers (5)

Eduardo Freitas
Eduardo Freitas

Reputation: 1

// Convert &lt; &gt; etc. to HTML
String sResult = HttpUtility.HtmlDecode(sData);
// Remove HTML tags delimited by <>
String result = Regex.Replace(sResult, @"enter code here<[^>]*>", String.Empty);

Upvotes: 0

naeemshah1
naeemshah1

Reputation: 142

try this

// erase html tags from a string
public static string StripHtml(string target)
{
//Regular expression for html tags
Regex StripHTMLExpression = new Regex("<\\S[^><]*>", RegexOptions.IgnoreCase |   RegexOptions.Singleline | RegexOptions.Multiline | RegexOptions.CultureInvariant | RegexOptions.Compiled);

return StripHTMLExpression.Replace(target, string.Empty);
}

call

string htmlString="<div><span>hello world!</span></div>";
string strippedString=StripHtml(htmlString);

Upvotes: 0

GMalla
GMalla

Reputation: 191

Assuming that:

  • the original string is always going to be in that specific format, and that
  • you cannot add the HTMLAgilityPack,

here is a quick and dirty way of getting what you want:

    static void Main(string[] args)
    {
        // Split original string on the 'separator' string.
        string originalString = "&lt;p&gt;LARGE&lt;/p&gt;&lt;p&gt;Lamb;<br>;li;ul;&nbsp;";
        string[] sSeparator = new string[] { "&lt;/p&gt;&lt;p&gt;" };
        string[] splitString = originalString.Split(sSeparator, StringSplitOptions.None);

        // Prepare to filter the 'prefix' and 'postscript' strings
        string prefix = "&lt;p&gt;";
        string postfix = ";<br>;li;ul;&nbsp;";
        int prefixLength = prefix.Length;
        int postfixLength = postfix.Length;

        // Iterate over the split string and clean up
        string s = string.Empty;
        for (int i = 0; i < splitString.Length; i++)
        {
            s = splitString[i];
            if (s.Contains(prefix))
            {
                s = s.Remove(s.IndexOf(prefix), prefixLength);

            }
            if (s.Contains(postfix))
            {
                s = s.Remove(s.IndexOf(postfix), postfixLength);
            }

            splitString[i] = s;
            Console.WriteLine(splitString[i]);
        }

        Console.ReadLine();
    }

Upvotes: 0

mybirthname
mybirthname

Reputation: 18127

If we assume that you are going to fix your html elements.

    static void Main(string[] args)
    {
        string html = WebUtility.HtmlDecode("&lt;p&gt;LARGE&lt;/p&gt;&lt;p&gt;Lamb&lt;/p&gt;");

        HtmlDocument doc = new HtmlDocument();
        doc.LoadHtml(html);

        List<HtmlNode> spanNodes = doc.DocumentNode.Descendants().Where(x => x.Name == "p").ToList();

        foreach (HtmlNode node in spanNodes)
        {
            Console.WriteLine(node.InnerHtml);
        }

    }

You need to use HTML Agility Pack.You can add reference like this.:

Install-Package HtmlAgilityPack  

Upvotes: 1

Mitch
Mitch

Reputation: 22251

I am going to assume that the HTML is intact, perhaps something like the following:

<ul><li><p>LARGE</p><p>Lamb<br></li></ul>&nbsp;

In which case, I would use HtmlAgilityPack to get the content without having to resort to regex.

var html = "<ul><li><p>LARGE</p><p>Lamb</p><br></li></ul>&nbsp;";
var hap = new HtmlDocument();
hap.LoadHtml(html);

string text = HtmlEntity.DeEntitize(hap.DocumentNode.InnerText);
// text is now "LARGELamb "

string[] lines = hap.DocumentNode.SelectNodes("//text()")
    .Select(h => HtmlEntity.DeEntitize(h.InnerText)).ToArray();
// lines is { "LARGE", "Lamb", " " }

Upvotes: 2

Related Questions