Malik.Akhtar Malik.Akhtar - 10 months ago 67
C# Question

How can i remove HTML Tags from String by REGEX?

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


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

Answer Source

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);


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